사용자가 한 행을 편집하여 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' ;
각 행에 대한 입력 상자의 이름이 정확히 동일합니다. 게시물에서 마지막 값으로 덮어 씁니다. –
가능한 중복 [ph p 업데이트 스크립트, 마지막 레코드 만 업데이트] (http://stackoverflow.com/questions/13728744/php-updates-last-record) – andy
Jonathan의 의견에 따르면, []]을 배열을 생성하기위한 필드 이름. '[ ". $ row [ 'eid']."]'할 수도 있습니다. 그래서 배열은 eid에 의해 색인 될 것입니다. –