2016-07-26 72 views
-2

我正在做一個應用程序來存儲學生記錄並編輯和刪除它們,但服務器發出未定義的變量錯誤(在$ name,$ school_name,$ roll_no,$ result變量行中),儘管變量已被定義並且我已經使用過回聲檢查和所有的變量似乎做工精細......好心幫我服務器發出未定義的變量錯誤?

<form action = 'edit.php?edit_form=<?php echo $edit_id; ?>' method = 'post'> 
     <table align = "center" width = "500" border = "5"> 
      <tr> 
       <td colspan = "5"> <h1 align = "center">Update Record</h1> </td> 
      </tr> 
      <tr> 
       <td align = "right">Student Name:</td> 
       <td> <input type = "text" name = "name" value = "<?php echo $name; ?>"> </td> 
      </tr> 
      <tr> 
       <td align = "right">School Name:</td> 
       <td> <input type = "text" name = "school" value = "<?php echo $school_name; ?>"> </td> 
      </tr> 
      <tr> 
       <td align = "right">Roll No:</td> 
       <td> <input type = "text" name = "roll_no" value = "<?php echo $roll_no; ?>"> </td> 
      </tr> 
      <tr> 
       <td align = "right">Result:</td> 
       <td> <input type = "text" name = "result" value = "<?php echo $result; ?>"> </td> 
      </tr> 
      <tr> 
       <td colspan = "5" align = "center"> <input type = "submit" name = "update" value = "Update Now"> </td> 
      </tr> 
     </table> 

    </form> 
+1

*什麼*變量未定義?你收到的確切消息是什麼? – tadman

+0

他們在哪裏定義? – Vuldo

+0

你能否給我們提供填充變量的代碼段? – pilec

回答

0

這實際上是有道理的,因爲你是嘗試設置變量呈現形式,這是在提交前,所以沒有POST請求中的數據。

FOW制定工作上的事情,你必須選擇從該數據庫的數據,是這樣的:

// mysql_connect is deprecated, use mysqli_connect instead 
$link = mysqli_connect("localhost", "root", "password"); 
mysqli_select_db($link, "school"); 

if (!isset($_GET['edit_form']) || !is_numeric($_GET['edit_form'])) { 
    // first validate user input from GET request 
    die('please provide us valid student id'); 
} 

// after successful updating showing confirmation message 
if (isset($_GET['show_message'])) { 
    echo "Data has been updated"; 
} 

$edit_id = $_GET['edit_form']; 

//using prepare statement for avoiding SQL injection 
$statement = $link->prepare('SELECT student_name, school_name, roll_no, result FROM students WHERE id = ?'); 


// i because student id should be integer 
$statement->bind_param('i', $edit_id); 

$statement->execute(); 
$statement->store_result(); 

// fill every needy variable from query 
$statement->bind_result($name, $school_name, $roll_no, $result); 
$statement->fetch(); 

if (isset($_POST['update'])) { 
    $name = $_POST['name']; 
    $school_name = $_POST['school']; 
    $roll_no = $_POST['roll_no']; 
    $result = $_POST['result']; 
    $query = "update students set student_name = ?, school_name = ?, roll_no = ?, result = ? where ID=?"; 

    // once again preparate query for avoiding SQL injection 
    $statement = $link->prepare($query); 

    // s for string variable, ssisi - 1st variable is string, 2nd variable is string, 3rd variable is integer ... 
    $statement->bind_param('ssisi', $name, $school_name, $roll_no, $result, $edit_id); 

    if ($statement->execute()) { 
     header("location: index.php?edit_form=$edit_id&show_message=1"); 
    } 
} ?>