PHP Delete Multiple Rows With Check Box

梦想的初衷 提交于 2019-12-08 12:42:08

问题


I'm Trying To Delete Multiple Rows By Using Check Boxes. But The Values Are Not Deleting. I Don't Understand Where I'm Going Wrong. Please Help Me.

Connect.php

    <?php
    $host="localhost"; // Host name 
    $username="DB_USERNAME"; // Mysql username 
    $password="DB_PASSWORD"; // Mysql password 
    $db_name="DB_NAME"; // Database name 
    $tbl_name="TABLE_NAME"; // Table name 

    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");

    ?>

exe.php

    <?php
    include "connect.php";

    // Check if delete button active, start this 
    if($_POST['delete'])
    {
    $id = $_POST['data'];
    $count = count($id);

    for($i=0;$i<$count;$i++){
    //echo "<br> value = ".$id[$i]."Jumlah = ".$count ;
    $sql = "DELETE FROM $tbl_name WHERE id='$id[$i]'";
    $result = mysql_query($sql);
    }

    // if successful redirect to delete_multiple.php 
    if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";}
    }
    mysql_close();
    ?>

index.php

    <?php
    include "connect.php";
    $result=mysql_query("SELECT * FROM $tbl_name ORDER BY ts DESC");
    ?>

    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td><form name="form1" method="post" action="exe.php">
    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td bgcolor="#FFFFFF"> </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>Order Id</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Username</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Link</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Type</strong></td>
    </tr>

    <?php
    while($rows=mysql_fetch_array($result)){
    ?>

    <tr>
    <td align="center" bgcolor="#FFFFFF"><input name="data[]" type="checkbox" id="data" value="<?php echo $rows['oid']; ?>">
    </td>
    <td bgcolor="#FFFFFF"><?php echo $rows['oid']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['user']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['data']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['type']; ?></td>
    </tr>

    <?php
    }unset($rows);
    ?>

    <tr>
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete">
    </td>
    </tr>
    </table>
    </form>
    </td>
    </tr>
    </table>

Someone please help me with this and tell me where am i wrong. I'm still learning PHP.


回答1:


Ill send 5 php files (8 with the css and js files) about this so you can use the codes yourself and comment if you have questions:

MULTI EDIT.RAR

This link also has its .sql file so you just need to import it and start testing.




回答2:


Instead of using a loop an running several SQL queries, you can just use the built in SQL WHERE IN clause:

$ids = join(',',$id);  //same as using implode()

$sql = "DELETE FROM $tbl_name WHERE id IN '$ids'";

It will be more efficient!

Hope this helps!



来源:https://stackoverflow.com/questions/21356382/php-delete-multiple-rows-with-check-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!