Search code examples
phpmysqlsql-deletedelete-row

Delete multiple records from database


I have a dashboard of product.On dashboard it display all product details.There is check box to delete multiple product at a time. For that I am using this code:

    $id=array('0'=> 'ab','1'=> 'bc','2'=> 'cd','3'=> 'de',.......);
    $packids=implode("','", $id);
    $packids= ('ab','bc','cd','ef',.....);// n numbers of record
    $sql = "DELETE from app_and_page where `Package` IN ($packids) and `user_created`=$uid";
    $retval = mysql_query( $sql, $link );

This query is deleting only one row.How can I delete all the selected rows at a time.


Solution

  • I thing the issue in pack ids just try this In place of

    $id=array('0'=> 'ab','1'=> 'bc','2'=> 'cd','3'=> 'de',.......);
    $packids=implode("','", $id);
    $packids= ('ab','bc','cd','ef',.....);// n numbers of record
    $sql = "DELETE from app_and_page where `Package` IN ($packids) and `user_created`=$uid";
    $retval = mysql_query( $sql, $link );
    

    Use this

    $id=array('0'=> 'ab','1'=> 'bc','2'=> 'cd','3'=> 'de',.......);
    $packids=implode("','", $id);
    $packids="('".$packids."')";
    $sql = "DELETE from app_and_page where `Package` IN $packids and `user_created`=$uid";
    $retval = mysql_query( $sql, $link );
    

    It will work for you.

    EDIT

    Try like this

    $sql = "DELETE from app_and_page where trim(`Package`) IN $packids and `user_created`=$uid";