我有一个 HTML 表单,用于通过 post 脚本更新 MySQL 表 - 但是,我遇到了一个问题,即每行上的更新按钮只适用于表中的最后一行。
<?php
$link = mysql_connect ('localhost', 'root', 'password');
mysql_select_db ('cardatabase');
$select = 'SELECT * from cars';
$result = mysql_query($select, $link);
if(isset($_POST['update'])){
$updatequery = "UPDATE cars SET
ID='$_POST[id]',
CARMAKE='$_POST[carmake]',
CARMODEL='$_POST[carmodel]',
FUELTYPE='$_POST[fueltype]',
TRANSMISSION='$_POST[transmission]',
DOORS='$_POST[doors]',
AMOUNT='$_POST[amount]',
AVAILABLE='$_POST[available]'
WHERE ID='$_POST[hidden]'";
mysql_query($updatequery, $link);
};
echo '<table><form action=update.php method=post>';
echo '<tr><td>ID</td><td>Make</td><td>Model</td><td>Fuel Type</td><td>Transmission</td><td>Engine Size</td><td>Doors</td><td>Amount</td><td>Available</td></tr>';
while($row = mysql_fetch_array($result)) {
echo '<tr>';
echo "<td><input type='text' name='id' value='{$row['ID']}'</td>";
echo "<td><input type='text' name='carmake' value='{$row['CARMAKE']}'</td>";
echo "<td><input type='text' name='carmodel' value='{$row['CARMODEL']}'</td>";
echo "<td><input type='text' name='fueltype' value='{$row['FUELTYPE']}'</td>";
echo "<td><input type='text' name='transmission' value='{$row['TRANSMISSION']}'</td>";
echo "<td><input type='text' name='enginesize' value='{$row['ENGINESIZE']}'</td>";
echo "<td><input type='text' name='doors' value='{$row['DOORS']}'</td>";
echo "<td><input type='text' name='amount' value='{$row['AMOUNT']}'</td>";
echo "<td><input type='text' name='available' value='{$row['AVAILABLE']}'</td>";
echo "<td><input type='hidden' name='hidden' value='{$row['ID']}'</td>";
echo '<td><input type="submit" name="update" value="Update"</td>';
echo '</tr>';
}
echo '</form></table>';
mysql_close ($link);
cars
桌子: