2014-11-20 41 views
-1

我已經編寫了這段代碼供用戶編輯一行並在MySQL中進行更新,但無論您選擇了哪一行,它總是發佈最後一行(有3行)。無法更新用戶選擇的行

有什麼問題?

<?php include("includes/db_connection.php"); ?> 

<?php 
global $connection; 

$sid="s5"; 



/**select all salesman from store 5**/ 
$sql ="SELECT * FROM employees WHERE e_type='Salesperson' AND store_assigned='".$sid."';"; 


/**get the result and put into table, which can be edited by user**/ 
$result = mysql_query($sql); 
echo "<form method='post' action='update_salesman.php'>"; 
echo "<table border='1'><tr><th>Employee ID</th><th>Name</th><th>Address</th><th>Email</th><th>Job Title</th><th>Store</th><th>Salary</th></tr>"; 
while ($row = mysql_fetch_assoc($result)) { 

    echo "<tr><td><input type='text' name='eid' value='".$row['eid']."' readonly /></td>"; 
    echo "<td><input type='text' name='e_name' value='".$row['e_name']."' /></td>"; 
    echo "<td><input type='text' name='e_addr' value='".$row['e_addr']."' /></td>"; 
    echo "<td><input type='text' name='e_email' value='".$row['e_email']."' /></td>"; 
    echo "<td><input type='text' name='e_type' value='".$row['e_type']."' /></td>"; 
    echo "<td><input type='text' name='store_assigned' value='".$row['store_assigned']."'/></td>"; 
    echo "<td><input type='text' name='e_salary' value='".$row['e_salary']."' /></td>"; 
    echo "<td><input type ='submit' value='update' /></td></tr>"; 

} 
echo "</table>"; 
echo "</form>"; 
print($sql); 
?> 

獲取發佈的數據,並在MySQL數據庫更新:

<?php include("includes/db_connection.php"); ?> 

<?php 
$eid = $_POST['eid']; 
$ename = $_POST['e_name']; 
$eaddr = $_POST['e_addr']; 
$eemail = $_POST['e_email']; 
$etype = $_POST['e_type']; 
$estore = $_POST['store_assigned']; 
$esalary = $_POST['e_salary']; 

$sql = "UPDATE employees SET e_name='" . $ename . "', e_addr='" . $eaddr . "', e_email='" . $eemail . "', e_type='" . $etype . "', store_assigned='" . $estore . "', e_salary='" . $esalary . "' WHERE eid='" . $eid . "' ;"; 
$result = mysql_query($sql); 
print("</br>" . $sql); 

?> 

結果總是這樣:

UPDATE employees SET e_name='Norah ', e_addr='111 Melwood,PA', e_email='[email protected]', e_type='Salesperson', store_assigned='s5', e_salary='4000.00' WHERE eid='e334' ; 
+2

您將每行的輸入框命名爲完全相同,在發佈後,它們將只覆蓋最後一個值 – 2014-11-20 19:39:13

+0

可能的重複[PH p更新腳本,只更新最後一條記錄](http://stackoverflow.com/questions/13728744/php-update-script-only-updates-last-record) – andy 2014-11-20 19:44:46

+0

根據喬納森的評論,你應該追加'[]'到字段名稱來創建一個數組。你也可以做'[「。$ row ['eid']。」]'這樣你的數組就可以被eid索引。 – 2014-11-20 19:48:46

回答

0

即使它可能出現在HTML側細線,很明顯服務器端發生了什麼,當它得到的形式

當服務器獲取形式我t只會看到最後一條記錄,因爲每條記錄都會覆蓋前面的值,從而導致只從最後一條記錄獲取數據。

您可以做的是給每組值設定自己的形式(不會建議)。但是用這種方法,你可以保持你的代碼幾乎不變,只需將表單標籤移動到while循環中即可。或寫輸入名稱作爲e_name []等

這樣,它會作爲數組服務器獲得通過,可以遍歷得到所有你的價值觀

在服務器端,以獲得陣列你會做類似

$e_names = $_POST['e_name']; //Value will be an array 
+0

謝謝,雙向工作! – 2014-11-21 19:20:37

1

你的問題是雙重的。首先,在生成HTML代碼時,您使用while循環來回顯字段。請注意,每次循環運行時,這些字段的名稱都是相同的。 (您可以在生成的HTML(源代碼)中看到這一點。請注意,在提交時,將會發布多個同名字段中的一個

其次,在PHP表單處理程序代碼中,然後執行一個更新查詢,而您可能需要更新多個字段

解決此問題的最簡單方法是確保HTML表單中的字段名對每個行都不相同,在更新sql表時使用循環結構,以便每行有一個更新