3

这是我的 php 代码来选择和删除表格中的行:

    <h1>Deleting Multiple Records using PHP &amp; MySQL</h1>
     <p>&nbsp;</p>
     <?php

这些是我的数据库连接:

  $host = "localhost"; // Host name
 $username = "root"; // Mysql username
 $password = ""; // Mysql password
 $db_name = "project"; // Changed database name
 $tbl_name = "users";
 mysql_connect($host, $username, $password) or die("cannot connect");
 mysql_select_db($db_name) or die("cannot select DB");
 $sql = "SELECT * FROM $tbl_name";
 $result = mysql_query($sql);
 $count = mysql_num_rows($result);
?>

这是我从数据库中检索数据的 html 代码:

   <table width="400" border="0" cellspacing="1" cellpadding="0">
  <tr><td><form name="form1" method="post" action="">
        <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"><tr>
            <td bgcolor="#FFFFFF">&nbsp;</td>
            <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>           </tr>
            <tr><td align="center" bgcolor="#FFFFFF">#</td>
            <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>Gender</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>Phone Number</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>Experiance</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>User Name</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>Password</strong></td>
            </tr>
            <?php while ($rows = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>

                <tr>
                <td align="center" bgcolor="#FFFFFF">
                    <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['fname']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['lname']; ?>   </td>
                <td bgcolor="#FFFFFF"><?php echo $rows['gend']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $rows['phone']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $rows['exp']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $rows['uname']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $rows['pwd']; ?>    </td>
                </tr>
            <?php } ?>
            <tr><td colspan="5" align="center" bgcolor="#FFFFFF">
                <input name="delete" type="submit" id="delete" value="Delete" onclick="javascript:confirm('Are you sure to delete')"></td></tr>
            <?php
            if(isset($_POST['delete'])){
                $checkbox = $_POST['checkbox'];
              for($i=0;$i<count($_POST['checkbox']);$i++){
           $del_id = $checkbox[$i];
            $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
             print $sql;
             $result = mysql_query($sql);}

               if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";}}
     mysql_close();
    ?>
    </table></form></td></tr></table>
     <p>Record count: <?php echo number_format($count) ?></p>*/

我尝试了很多次,但它没有从表中删除一行。刚才我更改了执行查询的代码....请检查并通知我...

4

3 回答 3

3

这是什么查询

$sql = "DEFRE FROM users WHERE id in (" . implode(",", $deleteIds) . ") ";

??

为了删除你应该做这样的事情

$sql = "DELETE FROM users WHERE id IN (" . implode(",", $deleteIds) . ") ";
于 2013-05-08T18:03:44.480 回答
1

在代码中

if (isset($_POST['checkbox']) && count($_POST['checkbox']) > 0)
{
     $deleteIds = $_POST['checkbox'];   // it will be an array
     $sql = "DELETE FROM users WHERE id in (" . implode(",", $deleteIds) . ") ";
     // run the query
 }

我们需要查看试图“删除”的实际代码。 常规的mysql_query($sql)不行。您必须使用mysql_exec()而不是mysql_query().

更新:对不起,我混淆了另一种形式的查询。 mysql_query("delete from table where (whatever)")应该可以工作,尽管它在成功时返回 true 或在失败时返回 false,而不是行集。

于 2013-05-08T18:19:50.900 回答
1

我从我的程序中删除了一个行 ID,我得到了答案,它工作正常。

                <tr>
                <td align="center" bgcolor="#FFFFFF">
                <input name="checkbox[]" type="checkbox" id="checkbox[]" value='<?php echo $row['id']; ?>'></td>
                <td bgcolor="#FFFFFF"><?php echo $row['fname']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $row['lname']; ?>   </td>
                <td bgcolor="#FFFFFF"><?php echo $row['gend']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $row['phone']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $row['exp']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $row['uname']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $row['pwd']; ?>    </td>
                </tr>
               <?php } ?>
                <tr><td colspan="5" align="center" bgcolor="#FFFFFF">
                <input name="delete" type="submit" id="delete" value="Delete" onclick="javascript:confirm('Are you sure to delete')"></td></tr>
               <?php

               if(isset($_POST['delete'])){
               $checkbox = $_POST['checkbox'];
            for($i=0;$i<count($_POST['checkbox']);$i++){
             $del_id = $checkbox[$i];
             //print '$del_id';
             $sql = "DELETE FROM users WHERE id='$del_id'";
          //print $sql;
           $result = mysql_query($sql);}
               }
      if($result){
         echo"successfully deleted";
         }
         mysql_close();
    ?>
    </table></form></td></tr></table>
    <p>Record count: <?php echo number_format($count) ?></p>   
于 2013-05-09T07:11:23.313 回答