Urvashi
Urvashi

Reputation: 239

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.

Upvotes: 1

Views: 395

Answers (2)

Bibhudatta Sahoo
Bibhudatta Sahoo

Reputation: 4894

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";

Upvotes: 1

Shimul
Shimul

Reputation: 87

$packids= ('ab','bc','cd','ef',.....);// n numbers of record $sql = "DELETE from app_and_page where Package IN (substr($packids, 0, strlen($packids)-1)) and user_created=$uid"; $retval = mysql_query( $sql, $link );

Try this one. I think you have to remove last comma from our packids string.

Upvotes: 0

Related Questions