1

I have the following code, which works to a certain degree:

if(isset($_POST['remove'])) {
    $user = $_POST['user'];
    $passcode = $_POST['passcode'];

    $sql = "SELECT * 
            FROM admin 
            WHERE username = '" . $user . "' 
            and passcode ='".md5($passcode)."'";

    $result = mysqli_query($conn,$sql);
    $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
    $count = mysqli_num_rows($result);
    if($count==0) {
        echo "Invalid Credentials";
    } else {

        include 'config.php';
        $cnt=array();
        $listCheck = "'" .implode("','", $_POST['checkbox']) . "'";
        $sql6 = "DELETE FROM `customer` WHERE `name` IN ($listCheck)";
        $delete = mysqli_query ($conn,$sql6);
    }

Issue is, it only deletes 1 of the selected rows. How do I modify the code so it deletes ALL of the selected rows?

<input type="submit" name="remove" class="btn2" value="Delete Selected">
<div>
<?php
$query1 = "SELECT `name` FROM `customer` ORDER BY `name` ASC ";
$result = mysqli_query($conn, $query1);

echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value=" . $row['name'] . "></td>";
echo"</tr>";
}
echo "</table>";
echo "<br>";
?>
</div>
8
  • result of var_dump($_POST['checkbox']) ? Commented Apr 24, 2017 at 12:21
  • 1
    Show us the HTML, specifically the <form> containing the Checkboxes in question Commented Apr 24, 2017 at 12:23
  • 1
    @RiggsFolly asked you to post your HTML over an hour ago; you failed to do that. How are we to know if that isn't failing? You're still at it with the answers given but we're not seeing an accepted answer. Commented Apr 24, 2017 at 13:58
  • 1
    md5($passcode)? I sure hope for your sake that this isn't a live site; you will get hacked. Commented Apr 24, 2017 at 14:01
  • 1
    Add ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser. Commented Apr 24, 2017 at 14:21

4 Answers 4

3

You can simply use one query to delete all records without the pain of for loop

 include 'config.php';
 $array = $_POST['checkbox'];
 $listCheck = "'" . implode("','", $array) . "'";
 echo $sql6 = "DELETE FROM `customer` WHERE `name` IN ($listCheck)";
 $delete = mysqli_query ($conn,$sql6);

Also there is issue in this code. Replace the below line with your code

echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='" . $row['name'] . "'></td>";
Sign up to request clarification or add additional context in comments.

13 Comments

Can you print the $sql6 ?
You are using the LIKE operator instead of IN. Also can you var_dump($_POST['checkbox'])
why are you using the for loop. I have added the answer to remove the loop. Please paste the var_dump of $_POST['checkbox']
@S.King Check this link i have created, your sql should be like this repl.it/HX0L
@S.King can you paste the output of $sql6
|
1

better go with foreach( $_POST['checkbox'] as $k => $v), it may not always be numbers and even if this way you do not have a loop for each possible in range but only for each selected checkbox.

Also have a look on prepared statements for SQL queries, you do not want to have possible injections.

Besides LIKE better be replaced by the exact comparison =.

2 Comments

"my code" will indeed not delete anything, it just provides the key and value of the list it iterates over… that you can then use to delete exactly what you need; anyways in this case the example of Agam is neat, solves the request and should be voted and marked by you ;)
of course, you wont have $i any more … you then have $k for key and $v for value of the list; maybe have a look into them e.g. with print_r() and discover the structure of your $_POST the same way, then you may begin to understand it.
1

Try the following code:

 include 'config.php';
 $array = $_POST['checkbox'];
 $listCheck = "";
 foreach($array as $arr) {
     $listCheck .= "'" . urldecode($arr) . "',";
 }
 $listCheck = substr(trim($listCheck), 0, -1);
 echo $sql6 = "DELETE FROM `customer` WHERE `name` IN ($listCheck)";
 $delete = mysqli_query ($conn,$sql6);

1 Comment

Does your $row['name'] contain the full name?
1

This is something that caught my eye

echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value" . $row['name'] . "></td>";

Should be

echo '<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="'.$row['name'].'" ></td>';

The value of the checkbox is missing an =

2 Comments

Alright gotcha. Using the users name in this situation might be risky. What happens if more people go by the same name? Always use an ID for this type of situation. If your table does not have one. Add it. This will fix your problem with spaces in names as well
Glad to hear it's solved. This could have easily been avoided if you used ID's tho ;-)

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.