0

I am trying to delete multiple rows with chekboxes. Below is my code

      <?php
$host="localhost"; // Host name 
$username="****"; // Mysql username 
$password="****"; // Mysql password 
$db_name="****"; // Database name 
$tbl_name="****"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$result = mysql_query("SELECT * FROM members WHERE dealer='Panzer Protection'");
?>
<form name="form1" method="post" action="">
      <?php
while($rows=mysql_fetch_array($result)){
?>
      <tr>
        <td bgcolor="#666666"><input name="checkbox[]" type="checkbox" id="checkbox[]"    
value="<? echo $rows['member_id']; ?>"></td>
        <td bgcolor="#666666"><? echo $rows['member_id']; ?></td>
        <td bgcolor="#666666"><center>
          <? echo $rows['member_msisdn']; ?></td>
        <td bgcolor="#666666"><center>
          <? echo $rows['member_name']; ?></td>
        <td bgcolor="#666666"><div align="center"><? echo $rows['dealer']; ?></div>   

 </td>
        <td align="center" bgcolor="#FFFFFF"><a href="control_clientinfo.php?member_id=   
<? echo $rows['member_id']; ?>" class="update">Look Up</a></td>
      </tr>
      <?php
}
?>

<tr>
<td colspan="6" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit"     
id="delete" value="Delete"></td>
</tr>

</form> //Forgot form close in past
<?php

// Check if delete button active, start this 
if($_POST['delete']){
for($i=0;$i<$count;$i++){
$i = 0;
while(list($key, $val) = each($_POST['checkbox'])) {
$sql = "DELETE FROM $tbl_name WHERE id='$val'";
mysql_query($sql);
$i += mysql_affected_rows();
}
}
// if successful redirect to 
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=control_clientlistdel.php\">";
}
}
mysql_close();
?>

It shows me the list i call and i can tick the boxes. If i hit delete button it just refreshes the screen and the one i ticked is still there

3
  • 2
    Your DELETE query is prone to SQL injection. Properly escape user contents or, better, learn about parametrized queries. Note that mysql_* functions are deprecated (see the red box). And use htmlspecialchars when outputting database fields to your HTML markup to prevent XSS. And the bgcolor and align attributes are deprecated. And the center element, too. Commented Mar 24, 2013 at 13:57
  • Where does your form close? Commented Mar 24, 2013 at 14:03
  • 1
    Also, to capture the button being clicked you should use something like $_POST["delete"]. Do NOT use just $delete. This used to be a method with the old PHP 4 and earlier but this is no longer accepted. Furthermore, you should check for the delete BEFORE you print the results. Otherwise it will print the records and then delete. Makes no sense Commented Mar 24, 2013 at 14:04

2 Answers 2

2

First things first. It's bad idea to use mysql as it is really old and it's deprecated.

Second, where do you assign your variables ($delete, $count)

you have to check if the delete key of your POST is set:

if (isset($_POST['delete'])) { // Then the form has been submitted

after this, assign your $count variable

$checkbox = $_POST['checkbox'];
$count = count($checkbox);

And everything must work fine.

Final result

if (isset($_POST['delete'])) {
    $checkbox = $_POST['checkbox'];
    $count = count($checkbox);

    for($i = 0; $i < $count; $i++) {
        $id = (int) $checkbox[$i]; // Parse your value to integer

        if ($id > 0) { // and check if it's bigger then 0
            mysql_query("DELETE FROM table WHERE member_id = $id");
        }
    }
}

Check out the mysqli and the PDO drivers for interacting with the database.

Sign up to request clarification or add additional context in comments.

2 Comments

@ Mirmko Akkov your method worked I just had a typo in $sql = "DELETE FROM $tbl_name WHERE member_id='$val'"; hust had id instead of member_id
Instead of performing a query delete one record, it's much faster to run 1 query that deletes all desired records. See my answer.
0

not sure if its a typo or not.. but you you have a missing from end tags and <table> in the posted code..

....
<td colspan="6" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
</form> //here

and you need to check the posted $delete value in if condition..the correct way is to use $_POST since you are using method as pos.. here method="post".

updated

 if(isset($_POST) && $_POST['delete']){ //here
     $count=count($_POST['checkbox']);
    for($i=0;$i<$count;$i++){
      $sql  = "DELETE FROM $tbl_name WHERE id='".$_POST['checkbox'][$i]."'";
      mysql_query($sql);

     }
}

you can use header() to redirect in php

 header( 'Location: http://www.yoursite.com/ontrol_clientlistdel.php' ) ;

4 Comments

Notice: Undefined index: if($_POST['delete']){ and yes i forgot the </form> when pasting
@TrevorAckermann: Use if (isset($_POST['delete'])) to test if delete exists in the POST array.
Now you only check if $_POST exists, not whether or not the delete index is present.
Notice: Undefined variable: for($i=0;$i<$count;$i++){ Error I am gettin now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.