Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams $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">
$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>";
                @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.
– Funk Forty Niner
                Apr 24, 2017 at 13:58
                md5($passcode)? I sure hope for your sake that this isn't a live site; you will get hacked.
– Funk Forty Niner
                Apr 24, 2017 at 14:01
                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.
– RiggsFolly
                Apr 24, 2017 at 14:21

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>";
                You are using the LIKE operator instead of IN. Also can you var_dump($_POST['checkbox'])
– Agam Banga
                Apr 24, 2017 at 13:26
                why are you using the for loop. I have added the answer to remove the loop. Please paste the var_dump of $_POST['checkbox']
– Agam Banga
                Apr 24, 2017 at 13:30

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 =.

"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 ;) – vv01f Apr 24, 2017 at 13:37 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. – vv01f Apr 24, 2017 at 13:45 $listCheck .= "'" . urldecode($arr) . "',"; $listCheck = substr(trim($listCheck), 0, -1); echo $sql6 = "DELETE FROM `customer` WHERE `name` IN ($listCheck)"; $delete = mysqli_query ($conn,$sql6);

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 =

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 – Michael Koelewijn Apr 25, 2017 at 11:43

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.