2

I'm having a problem with deleting data using checkbox. I actually, retrieve all the records that I want to show/display. But the Delete button doesn't work.

$sql="SELECT * FROM admin";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

<?php
    while($rows=mysql_fetch_array($result)){

    echo "<tr>";
        echo "<td>" . "<input type='checkbox' name='check[]' value='".$rows['id']."'/>" . "</td>";
        echo "<td>" . $rows['username'] . "</td>";
        echo "<td>" . $rows['email'] . "</td>";
    echo "</tr>";
?>

<input name="delete" type="submit" id="delete" value="Delete">

Here's the IF STATEMENT to check if the delete button was active.

if($delete){
    for($i=0;$i<$count;$i++){
        $del_id = $checkbox[$i];
        $sql = "DELETE FROM admin WHERE id='$del_id'";
        $result = mysql_query($sql);
    }
    // if successful redirect to delete_multiple.php 
    if($result){
        echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_admin.php\">";
    }
}

Any help is appreciated. Thank you!

11
  • What is $count? What is $checkbox? How are you outputting the checkboxes? You'll need to show more of your code... Commented Apr 22, 2015 at 10:44
  • 1
    do not use mysql_ functions use PDO or mysqli_ instead Commented Apr 22, 2015 at 10:47
  • Hi Mr. White, I already update the my question. Sorry for giving incomplete details Commented Apr 22, 2015 at 10:48
  • Please post the whole code of both (the input and delete), the fault can be everywhere. Commented Apr 22, 2015 at 10:53
  • try this if(isset($_post['delete'])){ //do something here } Commented Apr 22, 2015 at 10:54

2 Answers 2

1

You can IN function of MySql

if($delete){
    $checkboxIds = implode(',',$_POST['checkbox']);

    $sql = "DELETE FROM admin WHERE id IN ('".$checkboxIds."')";
    $result = mysql_query($sql);

    // if successful redirect to delete_multiple.php 
    if($result){
        echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_admin.php\">";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

While that's true, I believe it's not solving the problem. It should be suggested in a comment. Good approach however ;)
0

Try do it:

if(isset($_POST['delete']) { //check if you received the post

    for($i=0; $i<count($_POST['check']); $i++) { // count the elements

        $del_id = $_POST['check'][$i];

...

1 Comment

I don't know how to mark this thread as RESOLVED. Thank you so much Guilherme

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.