我需要你的建议。我从数据库获取数据到表:ID,名称。表中有操作:删除、启用、阻止。When action Delete is selected, I would like, that respectively record will be deleted. 但是,我的脚本不起作用并且总是删除最后一条记录,即使我选择了另一条记录。我认为问题是,所有记录的选择名称和隐藏输入名称都是相似的。但我找不到方法,如何用不同的名称创建它们。欢迎任何建议。
HTML:
<form method='post'>
<table border='1'>
<tr>
<th> ID </th>
<th> Name </th>
<th> Action </th>
</tr>
代码:
$db = new PDO('mysql:host=localhost;dbname=****;charset=utf8', '**', '**');
$query = $db->query("SELECT ID,statusas,login,vardas,email FROM users");
while($row = $query->fetch(PDO::FETCH_BOTH)) {
echo "<tr><input type='hidden' name='id' value='".$row[0]."'>";
echo "<td>".$row[0]."</td>";
//echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
//echo "<td>".$row[3]."</td>";
// echo "<td>".$row[4]."</td>";
echo "<td><select name='action'>
<option value='choose'>Choose..</option>
<option value='delete'> Delete </option>
<option value='enable'> Enable </option>
<option value='block'> Block</option>
</select></td>";
echo "</tr>";
}
echo "<br><input type='submit' name='submit'></table></form>";
if($_POST['submit']) {
if ($_POST['action']== 'delete') {
echo $_POST['id']; // delete query, but now I am just checking if I get a proper ID.
}
}
else {
echo "bad";
}