Problem to delete multiple row to mysql

hello all
i want to delete multiple row to mysql but not work.
here is my code: http://binboy.gigfa.com/

After clicking checkboxes for what you want to delete the records, you can submit the form by clicking the ‘delete’ button, the page this form is submitting, in that page use this code to delete the selected records:::
foreach(@$_POST[“checkbox”] as $key => $id)
{
$result=mysql_query(“DELETE FROM users WHERE UserID='”.(int)$id.“'”) or die(mysql_error());
}

I would suggest something along these lines

  1. generate a “delete” checkbox in your html form for each row of data retrieved from the database.
 
<?php 
 
while($row=mysql_fetch_assoc($rs)) { 
       echo '<input type="checkbox" name="delMe[]" value="'.$row['someID'].'" />'; 
}
 
?>

where someID is the id of the row in the database.

  1. create a “Delete selected items” button in your form.

  2. when the button in 2) is clicked an array of id’s to delete, called delMe, will be sent to the server side script.

  3. the server side script will receive the id’s to delete in $_POST[‘delMe’] (assuming method=“post”)

  4. process the array in 4) to delete the selected id’s from the database

 
<?php 
$rows2Del = $_POST['someID']; 
 
foreach($rows2Del as $id) { 
     $query = 'delete from myTable where fldID = "'.$id.'"'; 
     //then run the query
} 
?>

submitting a separate DELETE statement for each row to be deleted is quite wasteful

delete them all with just one DELETE statement

DELETE FROM users WHERE [COLOR="Blue"]UserID IN ( 3, 5, 9, 37 )[/COLOR]

yep that’s another way of doing it.

but for me it’s easier and less php code to do it the other way.

I don’t think it’s wasting much in terms of resources. it all happens in much less time than a heartbeat in my experience, unless your deleting 100’s of rows in 1 go.

thanks, but i call, why the following code does not work?
did you not bid following code!?
why this code not delete rows to database?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel=stylesheet type=text/css href="style.css"> <title>Untitled Document</title>
    <style type="text/css">
        .bigandbold { font-size: 20px; font-weight: bold; }
        .error { color: red; font-weight: bold; }
    </style>
</head>
<body>
<?php 
include('config.php');
// First delete the records necessary
$errDesc = "";
if(isset($_POST['delete'])) {
    $checkbox = $_POST['checkbox'];
    for($i=0;$i<$count;$i++) {
        $del_id = $checkbox[$i];
        $sql = "DELETE FROM test WHERE id='$del_id'";
        $result = mysql_query($sql);
        if (!$result) { $errDesc .= "<p>Error deleting $del_id";
    }
	}
    // if successful redirect to delete_multiple.php
    // NOTE:  If **this** page is delete_multiple.php, then this whole if statement is not needed..
if (strLen($errDesc) == 0) {
echo "<script language=\\"javascript\\">window.location=\\"display.php\\";</script>";    }
}     
$result = mysql_query("SELECT * FROM test ORDER BY name ASC");
$count=mysql_num_rows($result); 
?>
<div class="container">
    <div class="header">   Header  </div>
    <div class="nav4 bigandbold">1</div>
    <div class="nav4 bigandbold">#</div>
    <div class="content bigandbold">Name</div>
    <div class="nav3 bigandbold">LastName</div>
    <div class="sidebar bigandbold">Email</div>
<?php     
    if(strLen($errDesc) > 0){
        echo '<div class="error">'. $errDesc .'</div>';   
    }
$count=count($checkbox);   
while($row = mysql_fetch_array($result)) {
   echo '<div class="chekbox"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="'.$rows[id].'"></div>';
   echo '<div class="nav4">'. $row['id'] .'</div>';   
   echo '<div class="content"><a href="update.php?id='.$row['id'].'">'.$row['name'].'</a></div>';   
   echo '<div class="nav3">' . $row['url'] . '</div>';   
   echo '<div class="sidebar">' . $row['email'] . '</div>';   
}   
?>
    <div class="delete">
        <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
            <input name="delete" type="submit" value="delete">
        </form>
    </div>
    <div class="footer">Footer</div>
</div>
</body>
</html>

link code: http://binboy.gigfa.com/

why means,
the data gathering form database table is in outside of the form…
open form tag before the data displaying…

<div class=“delete”>
<form method=“post” action=“<?php $_SERVER[‘PHP_SELF’] ?>”>
<?php
while($row = mysql_fetch_array($result)) {
echo ‘<div class=“chekbox”><input name=“checkbox” type=“checkbox” id=“checkbox” value="’.$rows[id].‘"></div>’;
echo ‘<div class=“nav4”>’. $row[‘id’] .‘</div>’;
echo ‘<div class=“content”><a href="update.php?id=’.$row[‘id’].‘">’.$row[‘name’].‘</a></div>’;
echo ‘<div class=“nav3”>’ . $row[‘url’] . ‘</div>’;
echo ‘<div class=“sidebar”>’ . $row[‘email’] . ‘</div>’;
}
?>
<input name=“delete” type=“submit” value=“delete”>
</form>
</div>
<div class=“footer”>Footer</div>
</div>
</body>
</html>


$checkbox = $_POST['checkbox']; 
if(count($checkbox) > 0){
    $query = 'DELETE FROM test WHERE id IN (' . implode(',', $checkbox) . ')';
}

replaced but did not work.
See for yourself: http://binboy.gigfa.com/

it’s okay, rajug, let him do it the inefficient way, he’s a php expert after all

:smiley: :smiley:

your code for $query has more chars in it, albeit not many :wink:

I’m not sure what the issue is here. I said it was another way of doing it but I have my preferred way.

What would be a waste of my time is to go change the code in my existing classes to save maybe a few nano seconds of processing time.

yup

:smiley: :smiley:

you just need to do some basic debugging to find the line(s) causing the problem.

What is your purpose?

I was replying to rajug.

btw - have you done any debugging to find the first line that is causing a problem?

$checkbox = $_POST[‘checkbox’];
for($i=0;$i<$count;$i++) {
$del_id = $checkbox[$i];
$sql = “DELETE FROM test WHERE id=‘$del_id’”;
$result = mysql_query($sql);
if (!$result) { $errDesc .= “<p>Error deleting $del_id”;
}

you can replace with this code::::

foreach($_POST[“checkbox”] as $key => $id)
{
$result=mysql_query(“DELETE FROM test WHERE id='”.(int)$id.“'”) or die(mysql_error());
$result = mysql_query($sql);
if (!$result) { $errDesc .= “<p>Error deleting $id”;
}

thanks,i replace your code but not work.
See for yourself: http://binboy.gigfa.com/
i want after delete rows, show message successfully deleted . ex: you successfully deleted 3 rows.
codes please put the tag code

@binboy

I’m a little confused here :confused2:

are you looking for someone to debug it for you for free or are you trying to learn how to debug the code for yourself to improve your skills?

I’m starting to think it’s the former :wink:

no, i want to learn how to debug the code for yourself.
A lot of PHP do not know, What is debug?
Please explain step by step

it is showing message as ::

Error deleting \.

means your query problem.
check the query and column names also match.
this time only query problem…