2017-03-20 53 views
0

大家好,我試圖讓用戶在提交數據後編輯它們的數據。我有一個員工表設置了一個自動遞增的主鍵。這是我的代碼的這一點,這讓我很費心//getting id from url $StaffID = $_GET['StaffID'];我不能爲我的生活弄清楚它有什麼問題,因爲語法似乎是正確的。這是告訴我索引是未知的。無法編輯MySQL行

<?php 
// including the database connection file 
include_once("connect.php"); 

if(isset($_POST['update'])) 
{ 
    $StaffID = $_POST['StaffID']; 



    // checking empty fields 
    if(empty($Name) || empty($Address) || empty($Telephone) || empty($BusinessID)) {   
     if(empty($Name)) { 
      echo "<font color='red'>Name field is empty.</font><br/>"; 
     } 

     if(empty($Address)) { 
      echo "<font color='red'>Age field is empty.</font><br/>"; 
     } 

     if(empty($Telephone)) { 
      echo "<font color='red'>Email field is empty.</font><br/>"; 
     } 

     if(empty($BusinessID)){ 
      echo "<font color='red'>Email field is empty.</font><br>/"; 
     } 
    } else { 
     //updating the table 
     $result = mysqli_query($conn, "UPDATE staff SET Name='$Name',Address='$Address',Telephone='$Telephone', BusinessID='$BusinessID' WHERE StaffID = $StaffID"); 

     //redirectig to the display page. In our case, it is index.php 
     header("Location: HomePHP.php"); 
    } 
} 
?> 
<?php 
//getting id from url 
$StaffID = $_GET['StaffID'];  // <---- ERROR 

//selecting data associated with this particular id 
$result = mysqli_query($conn, "SELECT * FROM staff WHERE StaffID=$StaffID"); 

while($res = mysqli_fetch_array($result)) 
{ 


    $Name = $res['Name']; 
    $Address = $res['Address']; 
    $Telephone = $res['Telephone']; 
    $BusinessID = $res['BusinessID']; 
} 
?> 

回答

0

您應該先更改語法以防止SQL注入。我想你的問題將被解決。

$stmt = $mysqli->prepare("UPDATE staff SET Name= ?, Address= ?, Telephone= ?, BusinessID= ? WHERE StaffID = ?"); 
$stmt->bind_param("sssii", $Name, $Address, $Telephone, $BusinessID, $StaffID); 
$stmt->execute(); 
+0

Thankyou的答覆。在更新部分,我是否保留'?'你會在哪裏放置建議的代碼位? –

+0

是的,你保留'?','?'與下一行中的變量綁定在一起。只需將它替換爲你的代碼行「$ result = mysqli_query($ conn,」UPDATE staff ...「) – Kevin

+0

Thankyou非常感謝你的幫助,我正在學習很多東西,但不幸的是,它給我一個Unidentified index error //從網址獲取ID $ StaffID = $ _GET ['StaffID']; –