Forum Moderators: coopster

Message Too Old, No Replies

Having Trouble with Multiple Delete in PHP/ MySQL.

Multiple Delete in PHP/ MySQL

         

lonestar23

9:42 pm on Mar 7, 2008 (gmt 0)

10+ Year Member



The code below only seems to delete one record, and does not work when trying to do an array delete.. What the heck is wrong with the code? Thanks in Advance!

<?php
===QueryString===
delete.php?q=169¦170¦171¦

$arr = explode('¦', $q);

for ($i = 0; $i < count($arr); $i++) {
$query .= "DELETE FROM aCar WHERE aID =
'".$arr[i]."'";

$result =mysql_query($query);
}
?>

cameraman

11:23 pm on Mar 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried echoing the output from mysql_error() [us2.php.net] after each time the query runs?

lonestar23

12:52 am on Mar 8, 2008 (gmt 0)

10+ Year Member



I have tried the mysql_error() but to no avail... it is such a weird problem....

jatar_k

2:32 am on Mar 8, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



couple things

you don't need to count on every iteration, count once and store it in a var

$arrcnt = count($arr);
for ($i = 0; $i < $arrcnt; $i++) {

you're missing a $ in this line and if aID is an integer then you don't use ', you also shouldn't need the concatenation at the beginning, you should be replacing the value

$query = "DELETE FROM aCar WHERE aID = .$arr[$i];

so with changes and an or die for good luck

$arrcnt = count($arr);
for ($i = 0; $i < $arrcnt; $i++) {
$query = "DELETE FROM aCar WHERE aID = .$arr[$i];
$result =mysql_query($query) or die($i . ':' . mysql_error());
}

lonestar23

3:35 am on Mar 8, 2008 (gmt 0)

10+ Year Member



thank you all so much... after a day of tinkering with my original code I decided to abandon the checkboxes (Multiple Delete Array). I will be using the code in the future, so it will come in handy!

lonestar23

10:29 pm on Mar 8, 2008 (gmt 0)

10+ Year Member



jatar_k your solution worked wonderfully! Time to implement into my code! Cheers!