简体   繁体   English

从表中删除错误的记录

[英]Delete wrong record from table

I need your advice. 我需要你的建议。 I fetch data from database to table: ID, Name. 我从数据库获取数据到表:ID,名称。 In table are Actions: Delete, Enable, Block. 表中有Actions:Delete,Enable,Block。 When action Delete is selected, I would like, that respectively record will be deleted. 当选择动作删除时,我想,将删除相应的记录。 However, my script does not work and always delete last record, even I select another record. 但是,我的脚本不起作用并且总是删除最后一条记录,即使我选择了另一条记录。 I think problem is, that select name and hidden input name is similar for all records. 我认为问题是,所有记录的选择名称和隐藏输入名称都是类似的。 But I can not find way, how to create them with different names. 但我找不到方法,如何用不同的名字创建它们。 Any advice is welcome. 欢迎任何建议。

HTML: HTML:

  <form method='post'> 
  <table border='1'>
  <tr>
  <th> ID </th>
<th> Name </th>
<th> Action </th>
</tr>

Code: 码:

$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";
}

表

You are using the same name attribute on every row in the form, so they are being overridden and it's using the last one. 您在表单中的每一行上使用相同的name属性,因此它们被覆盖并且使用最后一个。

What you could do is either wrap every row in its own form, or you could do something like this, and have only 1 submit button to execute the action on every row: 你可以做的是以自己的形式包装每一行,或者你可以做这样的事情,并且只有一个提交按钮来执行每一行的操作:

// remove hidden id element

...

echo "<td><select name='action[" . $row[0] . "]'>"

...
// remove submit button in the loop, but add it after the while loop
...

if (isset($_POST['action']))
{
    foreach ($_POST['action'] as $id => $action)
    {
        if ($action !== 'choose')
        {
            // do action on the id;
            echo $id . " -> " . $action . "<br>";
        }
    }
}

You put every ID in the form, which results in the CGI to send something like this: id=1, id=2, id=3, etc. PHP then only reads the last ID and deletes that. 你把每个ID放在表单中,这导致CGI发送这样的东西:id = 1,id = 2,id = 3等。然后,PHP只读取最后一个ID并删除它。

To fix it, give each row its own form. 要修复它,请为每行添加自己的表单。

while($row = $query->fetch(PDO::FETCH_BOTH)) { 
 echo "<form method='post'>";
 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 "</form>";
 }
 echo "<br><input type='submit' name='submit'></table>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM