我已經編寫了這段代碼供用戶編輯一行並在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' ;
您將每行的輸入框命名爲完全相同,在發佈後,它們將只覆蓋最後一個值 – 2014-11-20 19:39:13
可能的重複[PH p更新腳本,只更新最後一條記錄](http://stackoverflow.com/questions/13728744/php-update-script-only-updates-last-record) – andy 2014-11-20 19:44:46
根據喬納森的評論,你應該追加'[]'到字段名稱來創建一個數組。你也可以做'[「。$ row ['eid']。」]'這樣你的數組就可以被eid索引。 – 2014-11-20 19:48:46