行不会被删除

时间:2013-05-08 18:02:37

标签: php mysql

这是我的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>*/

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

3 个答案:

答案 0 :(得分:3)

这是什么查询

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

...

要删除你应该做这样的事情

$sql = "DELETE FROM users WHERE id IN (" . implode(",", $deleteIds) . ") ";

答案 1 :(得分: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,而不是行集。

答案 2 :(得分: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>