2
请原谅我的结构严重的代码。我试图用个人提交按钮更新每行上的不同客户。删除功能很好。我试图在每个行单击提交按钮时传递该行的ID。但是,无论我点击哪一行,我都会得到最后一行的ID。我附上了一个截图作为例子。任何帮助表示赞赏!单个表单上的多个更新按钮
<form action="" method="post">
<table class="table table-striped table-bordered table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>Product</th>
<th>Firmware Version</th>
<th>Purchase Date</th>
<th>Delete</th>
</tr>
</thead>
<?php
$pdo = new PDO("mysql:host=localhost;dbname=project", $username, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query = $pdo->prepare("select * from customers");
$query->execute();
while($customers = $query->fetch()){
$ID = $customers['ID'];
echo '<tr><td><input type="text" name="name" value="';
echo $customers['name'];
echo '"></td>';
echo "<td>" . $customers['email'] . "</td>";
echo "<td>" . $customers['phone'] . "</td>";
echo "<td>" . $customers['address'] . "</td>";
echo "<td>" . $customers['product'] . "</td>";
echo "<td>" . $customers['firmware'] . "</td>";
echo "<td>" . $customers['purchase_date'] . "</td>";
echo '<td align="center"><input type="hidden" name="id" value="';
echo $ID;
echo '"><input type="submit" name="delete" value="X"> </td></tr>';
echo '<tr><td colspan="8"><input type="hidden" name="id_update" value="';
echo $ID;
echo '"><input type="submit" name="update" value="Update">';
echo $ID . '<--This is the ID for each row';
echo '</td></tr>';
}
// Delete customer
if(isset($_POST['delete'])) {
try{
$ID = $_POST['id'];
$query = $pdo->prepare("delete from customers where ID = :ID");
$query->bindParam(':ID', $ID);
$query->execute(array(
':ID' => $ID
));
echo "Customer successfully deleted.";
echo '<META http-equiv="refresh" content="1;URL=view_edit.php">';
}catch(PDOException $e){
echo "Failed to delete the MySQL database table ... :".$e->getMessage();
} //end of try
} //end of isset delete
// Edit customer
if(isset($_POST['update'])) {
echo "Update " . $_POST['id_update'] . '<-- This is the result of clicking update for each row';
} //end of isset update
?>
</table>
</form>
That worked !!!非常感谢!你的方式肯定更有意义。 – eod
嘿没问题!很高兴我能帮助它工作! – Rasclatt