IT的各位高人們你們好
我想更新table內多筆欄位資料
我用foreach在A頁面將資料庫的資料依序取出來並顯示在textbox內
並在textbox內進行更改
按下update後會跳至B頁面執行更新的動作回傳資料庫
後來有到資料庫查看 但不管更新多少筆
目前只會更新最後一筆資料,其他筆資料並不會更新,請問該怎麼改才能達到我的期望呢?....
本身程式方面知識不足但有努力爬過文章,但沒有什麼進展...請各位協助
A.php的部分code
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<form id="form3" name="form3" action="B.php" method="POST">
<table border=1>
<tr>
<td>設備ID</td>
<td>設備名稱</td>
<td>座標x</td>
<td>座標y</td>
<td><input type=submit name=up id=up value=update ></td>
</tr>
<?php
foreach( $response as $value ):
echo"<tr>";
echo"<td><Input Type=text Name=id value=$value[id] readonly ></td>";
echo"<td><Input Type=text Name=name value=$value[name] ></td>";
echo"<td><Input Type=text Name=x value=$value[x] ></td>";
echo"<td><Input Type=text Name=y value=$value[y] ></td>";
echo"</tr>";
endforeach;
?>
</table>
</body>
</html>
B.php接收端
<?php
include 'data.php';
try{
$dst_conn = new mysqli($host, $username, $password, $db_name);
$err = '';
$sql = "Update table Set name = '$_POST[name]', x = '$_POST[x]', y = '$_POST[y]' WHERE id = '$_POST[id]'";
$result = $dst_conn->query($sql);
$response = array();
$idx=0;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$response[$idx]["id"]=$row['id'];
$response[$idx]["name"]=$row['name'];
$response[$idx]["x"]=$row['x'];
$response[$idx]["y"]=$row['y'];
$idx = $idx+1;
}
}
}
catch (PDOException $e) {
$err = "Error: " . $e->getMessage();
print $err;
}
//header( "location:index.php");
?>
<?php
// 前端
foreach($response as $value):
echo '<tr>';
echo '<td><input type="text" name="res[' . $value['id'] . '][id]" value="' . $value['id'] . '" readonly ></td>';
echo '<td><input type="text" name="res[' . $value['id'] . '][name]" value="' . $value['name'] . '"></td>';
echo '<td><input type="text" name="res[' . $value['id'] . '][x]" value="' . $value['x'] . '"></td>';
echo '<td><input type="text" name="res[' . $value['id'] . '][y]" value="' . $value['y'] . '"></td>';
echo '</tr>';
endforeach;
?>
<?php
// 後端
foreach($res as $rId as $rData):
$sql = 'UPDATE table SET name = ' . $rData['name'] . ', x = ' . $rData['x'] . ', y = ' . $rData['y'] . ' WHERE id = ' . $rId . ' LIMIT 1';
$result = $dst_conn->query($sql);
endforeach;
?>