2014-01-20 31 views
-2

我正在研究將信息拉入表單的Web後端,然後在更新時將使用新信息更新數據庫。但是,當我試圖提取以前存儲在類私有變量中的信息時,它會引發錯誤,指出信息爲NULL。我在這裏做錯了什麼?類變量NULL?

<?php 
class modify_racer 
{ 
    private $mysqli, $racer_id, $firstname, 
     $lastname, $banner, $bio; 

    public function error($code) 
    { 
     switch($code) 
     { 
      case 1: 
       echo '<p id="error"><b>Error:</b> Please fill out all fields!</p>'; 
       modify_racer::send_form($this->firstname, $this->lastname, $this->banner, $this->bio); 
       break; 
      case 2: 
       echo '<p id="error"><b>Error:</b> Racer already exists!</p>'; 
       break; 
      case 3: 
       echo '<p id="error"><b>Error:</b> Could not connect to MySQLi: ' . mysqli_error(); 
       break; 
     } 
    } 

    public function send_form($modify = 1) 
    { 
?> 

<div id="form"> 
    <h3>Edit Racer:</h3> 
    <form method="post" action=""> 
     <label for="firstname">First Name: </label> 
     <input type="text" id="firstname" name="firstname" 
      placeholder="Racer's First Name" 
      value="<?php echo $this->firstname;?>" /> 
     <br /> 
     <label for="lastname">Last Name: </label> 
     <input type="text" id="lastname" name="lastname" 
      placeholder="Racer's Last Name" 
      value="<?php echo $this->lastname;?>" /> 
     <br /> 
     <label for="banner">Banner Location: </label> 
     <input type="text" id="banner" name="banner" 
      placeholder="Racer's Banner Image Location:" 
      value="<?php echo $this->banner;?>" /> 
     <br /> 
     <label for="bio">Racer's Bio Info: </label> 
     <textarea rows="5" cols="50" id="bio" name="bio" 
      placeholder="Racer Statistics/Biography" 
      value=""><?php echo $this->bio;?></textarea> 
     <input type="submit" id="submit" name="modify" value="submit" /> 
    </form> 
</div> 

<?php 
    } 

    public function get_racer($racerID) 
    { 
     $this->racer_id = $racerID; 

     $this->mysqli = new mysqli(MYSQLI_HOST,MYSQLI_USER,MYSQLI_PASS,MYSQLI_DATABASE) 
      or die(error(3)); 

     $racer_info = "SELECT * FROM ArtecRacers WHERE RacerID=?"; 
     $load_racer = $this->mysqli->prepare($racer_info); 
     $load_racer->bind_param('s', $racerID); 
     $load_racer->execute(); 
     $load_racer->bind_result($this->racerID, $this->firstname, $this->lastname, $this->banner, $this->bio); 
     $load_racer->fetch(); 

     modify_racer::send_form(); 
    } 

    public function list_racers() 
    { 
?> 

<div id="form"> 
    <h3>Select Racer:</h3> 
    <form method="post" action=""> 

     <?php 
      $this->mysqli = new mysqli(MYSQLI_HOST,MYSQLI_USER,MYSQLI_PASS,MYSQLI_DATABASE) 
       or die(error(3)); 
      $racer_list = "SELECT * FROM ArtecRacers"; 

      $get_racers = $this->mysqli->query($racer_list); 

      while($list = $get_racers->fetch_array(MYSQLI_NUM)) 
      { 
       echo '<input id="part" type="radio" name="editRacer" value="' . $list[0] . '"/>'; 
       echo '<label for="part">' . $list[1] . ' ' . $list[2] . '</label><br />'; 
      } 
     ?> 

     <input type="submit" name="selectRacer" id="submit" value="Select Racer" /> 
    </form> 
</div> 

<?php 
    } 

    function test2() 
    { 
     echo $this->firstname; 
     echo $this->lastname; 
     echo $this->racer_id; 
    } 
} 

$start = new modify_racer(); 


if(!isset($_POST['selectRacer'])) 
    $start->list_racers(); 

if(isset($_POST['selectRacer'])) 
    $start->get_racer($_POST['editRacer']); 

$start->test2(); 

?> 

代碼中的所有內容除了$ start-> test2();從函數test2()中抽取的所有信息都是空白的,我不知道爲什麼......任何見解?

編輯:

我改變了代碼,以反映在底部,和TEST2(以下)還是輸出變量爲NULL:

if(!isset($_POST['editRacer'])) 
    $start->list_racers(); 
else 
    $start->get_racers($_POST['editRacer']); 

$start->test2(); 
+0

你可以試試'$開始 - > $ this-> test2();' –

+0

試試這個:http://stackoverflow.com/questions/6575482/how-do-i-enable-error-reporting-in- php –

+1

@ Fred-ii-:我不認爲這是有效的。 –

回答

1

如果你獨自離開你的代碼,你將不得不通過selectRacereditRacer參數到頁面中。我的猜測是,你可能只想通過那個。在這種情況下,你會想改變

if(isset($_POST['selectRacer'])) 
    $start->get_racer($_POST['editRacer']); 

if(isset($_POST['editRacer'])) 
    $start->get_racer($_POST['editRacer']); 

另外,如果你想通過地址欄傳遞這些價值,你需要檢查$_GET$_POST

最後,無處不在,你是通過執行modify_racer::my_method_here()製作方法調用,你應該改變,要$this->my_method_here()。前者是一個靜態方法調用,這意味着它實際上並不與您的對象關聯,這意味着它無法觸及這些變量。爲了能夠訪問和更改變量,您需要通過$this來調用它。

+0

我已經嘗試應用更改,並且我仍然有問題。如果(!isset($ _ POST ['editRacer'])) $ start-> list_racers();最後我改變了所有靜態方法,並且將底部的原始if語句更改爲: 。 else $ start-> get_racer($ _ POST ['editRacer'])); 這工作正常,但如果我調用test2(),它仍然輸出爲NULL三個變量。 – TheSilentDrifter

+0

當你調用內部調用'send_form()'的'get_racer()'時,表單是否也缺少所有的數據,或者只有當你調用'test2()'時纔會丟失?你是否通過URL欄傳遞了'editRacer'參數? – JMTyler

+0

這兩個函數中的所有數據都是不變的。只有當我調用test2()時,數據才返回爲NULL。我沒有通過URL欄傳遞參數。 – TheSilentDrifter