Hi all!
I have a list of contacts with checkbox in every row. I want to delete multiple contacts via the checkbox but with no success.

It would be greatly appreciated if anyone could help. Thanks in advance.

      <form name="form1" method="post" action="">
        <p>
          <?php

    $cName = trim($_POST['cname']);

    //echo "Cname is ".$cName;

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error in connecting ");

    mysql_select_db($dbname) or die ("Error in using database");

    $qur = "SELECT * FROM contacts";

    if($cName != ""){ //user does not want to search for all contact.  some thing was entered in txt box
        //$qur .= " WHERE contactname ='".$cName."'";
        $qur .= " WHERE contactname LIKE '%".$cName."%'";
    }

    $qur .= " ORDER BY userid"; //sort

    //echo "qur is".$qur;
    //Execute query and store records returned by SELECT
    $res = mysql_query($qur);

    //Check if any results are returned
    if(mysql_num_rows($res) == 0){
        echo "<i>No Staff contacts found.</i><br>";
    } //if
    elseif (mysql_num_rows($res) > 0) { //cannot be -1!!!
        //fetch the rows as assoc array

        echo "<table border = 1 width=70% align='center' >";
        echo "<th><input type='checkbox' name='sAll' onClick='checkUncheckAll(this);'/>".$row['']."</th><th>ID</th><th>Name</th><th>E-mail Address</th><th>Contact Number</th><th>Edit</th>";
        while($row = mysql_fetch_array($res)){
            //one table row (tr) for each record in record set res
            $ii = $row['userid'];
            echo "<tr><td align='center'><input type='checkbox' id='checkbox[]' name= 'checkbox[]' value= '".$row['userid']."'/>".$row['']."</td><td align='center'>".$row['userid']."</td><td align='center'>".$row['contactname']."</td><td align='center'>".$row['email']."</td><td align='center'>".$row['phone']."</td><td align='center'>
            <a href='edit-contact.php?userid=".$ii."'>Edit</a></td></tr>";


// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_userid = $checkbox[$i];
$sql = "DELETE FROM contacts WHERE userid='$del_userid'";
$res = mysql_query($sql);
}

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


        }//end while
        echo "</table>";

    } //else

} //if isset() 

else { //trying to access this page without logging-in
    echo "<center>";
    echo "<br>";
    echo "You are not authorized to view this page!";
    echo "</br>";
    echo "<br>";
    echo "<br>";
    echo "<a href='index.html'>Log in</a>";
    echo "</br>";
    echo "</br>";
    echo "</center>";
    die ("");
} //else 
//close database connection
include 'closedb.php';
?>
        </p>
        <pre>                                                              <input type="submit" name="delete" id = "delete" value="Delete">         
        </pre>
        <p>

        </p>
      </form>

Edited 3 Years Ago by Nick Evan: Fixed formatting

You should probably use implode, something like this:

foreach($_POST['userid'] as $userid)
{
	if(!is_numeric($userid))
	{
		header("location: " . $_SERVER['PHP_SELF'] . "?error=true");
		exit();
	}
}
$query = "delete from table where userid in(" . implode(",", $_POST['userid']) . ")";

You should probably use implode, something like this:

foreach($_POST['userid'] as $userid)
{
	if(!is_numeric($userid))
	{
		header("location: " . $_SERVER['PHP_SELF'] . "?error=true");
		exit();
	}
}
$query = "delete from table where userid in(" . implode(",", $_POST['userid']) . ")";

I have insert the following codes. It shows
Warning: implode() [function.implode]: Bad arguments.
Warning: Invalid argument supplied for foreach()

Please Help!

Where are you assigning a value to $count, $delete and $checkbox ? If register globals are turned on(which is off by default), $checkbox and $delete will work.
Does it even enter this condition ?

for($i=0;$i<$count;$i++){
$del_userid = $checkbox[$i];
$sql = "DELETE FROM contacts WHERE userid='$del_userid'";
$res = mysql_query($sql);
}

And, you should use $_POST in Rob's code.

$query = "delete from table where userid in(" . implode(",", $_POST['checkbox']) . ")";
This article has been dead for over six months. Start a new discussion instead.