0

我有一个查询,它显示一个包含有关消息的信息的表格,我还有一个列,一旦他们勾选了他们想要删除的消息,就可以选择用户想要使用复选框删除的消息,然后,他们单击提交按钮,将他们带到处理页面,在该页面中执行删除每个选定消息的 SQL 语句;但是,该声明似乎不起作用,这是我到目前为止所拥有的:

消息显示页面:

if ($_SESSION['user_session'] == "$current_user")
{
    {
     echo "<table border='1'>
     <tr>
     <th>Message ID</th>
     <th>Username</th>
     <th>Subject</th>
     <th>Message</th>
     <th>Delete?</th>
     </tr>";

        while($row = mysql_fetch_array($result))
         {
          echo "<tr>";
          echo "<td>" . $row['message_id'] . "</td>";
          echo "<td>" . $row['username'] . "</td>";
          echo "<td>" . $row['sub'] . "</td>";
          echo "<td>" . $row['message'] . "</td>";
          echo "<td><input type='checkbox' name='check_list[]' value='" .     $row['message_id'] . "' /></td>";
          echo "</tr>";
             }
      echo "</table>";
     }
}

带有 SQL 语句的进程页面:

if(!empty($_POST['check_list']))
{
    foreach($_POST['check_list'] as $check)
    {
    //connection to the database
    $dbhandle = mysql_connect("XXX", "XXX", "XXX")
    or die("Unable to connect to MySQL");

    //select a database to work with
    $selected = mysql_select_db("XXX",$dbhandle)
    or die("Could not select examples");

    //execute the SQL query to update the "returned" field to 1 wherever the loan_id is     checked.
    $result = mysql_query("DELETE FROM message WHERE message_id='$check'");
    }

              if ($result)
          {
              echo 'Messages Deleted';
          }
}
4

2 回答 2

0
if(!empty($_POST['check_list']))
{
    $dbhandle = mysql_connect("XXX", "XXX", "XXX") or die("Unable to connect to MySQL");
    $selected = mysql_select_db("XXX",$dbhandle) or die("Could not select examples");

    $result = mysql_query("DELETE FROM message WHERE message_id IN (" . implode(',', $_POST['check_list']) . ")", $dbhandle);
    if ($result !== false)
        echo 'Messages deleted.';
}

$_POST['check_list']您显然应该在执行查询之前对 的内容进行更多测试。我们不希望邪恶的人做傻事。

于 2013-01-24T17:05:24.423 回答
0

据我所知,您的代码没有任何问题,您应该var_dump($check)在 SQL 查询之前尝试 a 以输出它包含的内容,并var_dump($_POST['check_list'])在第二页的开头尝试 a。

另外,我建议您在进行多个不必要的连接时取出数据库连接foreach,如下所示:

if(!empty($_POST['check_list'])) {
    // Output the variable $_POST['check_list']
    var_dump($_POST['check_list']);

    //connection to the database
    $dbhandle = mysql_connect("XXX", "XXX", "XXX") or die("Unable to connect to MySQL");

    //select a database to work with
    $selected = mysql_select_db("XXX",$dbhandle) or die("Could not select examples");

    foreach($_POST['check_list'] as $check) {
        // Output the $check variable to see what it contains
        var_dump($check);

        //execute the SQL query to update the "returned" field to 1 wherever the loan_id is     checked.
        $result = mysql_query("DELETE FROM message WHERE message_id='$check'");

        // For each $check, verify what message_id got deleted
        if ($result) {
            echo 'Message ID Deleted: ' . $check;
        }
    }
}
于 2014-01-30T05:00:16.703 回答