I want to delete multiple users from db table with checkboxes at once. But my code doesn’t work correctly
index.php
<?php
require 'db.php';
$query="SELECT * FROM usr_table";
$result = $db->query($query) or die(mysqli_error($mysqli));
$num=mysqli_num_rows($result) or die(mysqli_errno());
if ($result) {
echo '
<form method="post" action="delete.php">
<table id="list">
while ($row = $result->fetch_object()) {
$id = $row->id;
$fullname = $row->fullname;
$dob = $row->dob;
$phone= $row->phone;
$adress= $row -> adress;
$school = $row->school;
echo '<tr>
<td>'.$id.'</td>
<td>'.$fullname.'</td>
<td>'.$dob.'</td>
<td>'.$phone.'</td>
<td>'.$adress.'</td>
<td>'.$school.'</td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value=$id />
</tr>';
}
// when the loop is complete, close off the list.
echo "</table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p></form>";
}
?>
delete.php
<?php
require 'db.php';
$delete=$_GET['delete'];
if($delete) // from button name="delete"
{
$checkbox = $_GET['checkbox']; //from name="checkbox[]"
$countCheck = count($_GET['checkbox']);
for($i=0;$i<$countCheck;$i++)
{
$del_id = $checkbox[$i];
$sql = "DELETE from usr_table where id = '$del_id'";
$result = $db->query($sql) or die(mysqli_error($db));
}
if($result)
{
header('Location: index.php');
}
else
{
echo "Error: ".mysqli_error($db);
}
}
?>
db.php
<?php
$db = new mysqli('localhost', 'user' ,'pass', 'table') or die(mysqli_errno());
$db->query("SET names UTF8") or die(mysqli_errno());
foreach ($_POST as $key => $value) {
$_POST[$key] = mysqli_real_escape_string($value);
}
?>
Please help! It doesn’t work
HTML version
<form method="post" action="delete.php">
<table id="list">
<thead>
<tr>
<th width="5%">ID</th>
<th width="35%">Ad və soyad</th>
<th width="15%">Təvəllüd</th>
<th width="15%">Telefon</th>
<th width="20%">Ünvan</th>
<th width="5%">Məktəb</th>
<th width="5%">#</th>
</tr>
</thead><tr>
<td>20</td>
<td>Tural Teyyuboglu</td>
<td>1992-06-09</td>
<td>4940301</td>
<td>Baki Yasamal Zergerpalan</td>
<td>189</td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="20 "/>
</tr><tr>
<td>22</td>
<td></td>
<td>1992-06-09</td>
<td>0</td>
<td></td>
<td></td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="22 "/>
</tr><tr>
<td>23</td>
<td>Tural Turik</td>
<td>1992-06-09</td>
<td>4940301</td>
<td>Bakı Yasamal Zərgerpalan</td>
<td>189</td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="23 "/>
</tr><tr>
<td>24</td>
<td>Tural Əliyev</td>
<td>1992-06-09</td>
<td>4940301</td>
<td>Bakı Y </td>
<td></td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="24 "/>
</tr><tr>
<td>25</td>
<td>Zammbbb</td>
<td>1992-06-09</td>
<td>4940301</td>
<td>Bakı Uadnf` skbnfias</td>
<td>189</td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="25 "/>
</tr><tr>
<td>26</td>
<td>Tural Əliyev</td>
<td>1992-06-09</td>
<td>0</td>
<td> </td>
<td></td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="26 "/>
</tr></table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p></form> </td>
</tr>
</table>
There are a few things wrong here..
1) Your form
method="POST". In your delete.php, you are using$_GET['checkbox'].Use
$_POST['checkbox']instead. ($_GET['delete']needs to be$_POST['delete']).2) You’re echoing PHP code:
You need to end your echo statement before continuing with PHP code:
3 & 4) Put quotes around your values in your input element, like so:
And if you’re going to echo like that, then escape your double quotes since it is in an echo statement:
5) Your
$_POSTsanitizing is what is emptying your multi-dimensional $_POST[‘checkbox’] array fromdb.php.When I remove this from
db.phpThen your
print_r($_POST)should display your values. Assuming everything else is alright, that should work. However, you’ll need to reconsider how you sanitize your POST variables.