How to delete multiple data from table with checkbox?
I have this code:
<form id="prefForm" action="coments-del.php" method="post">
<?
$result = mysql_query("SELECT * FROM comments");
$myrow = mysql_fetch_array($result);
do
{
$res = mysql_query("SELECT title FROM data WHERE id=$myrow[post]");
$row = mysql_fetch_array($res);
printf ("
<tr>
<td>
<input type='checkbox' name='id' value='%s' />
</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
",$myrow["id"],$myrow["id"],$myrow["author"],$myrow["text"]);
}
while ($myrow = mysql_fetch_array($result));
?>
<input name="submit-button" type="submit" value="Delete" />
</form>
Here is the coments-del.php where you get brought after submiting form in script above.
In the head is:
if (isset($_POST['id'])) { $id = $_POST['id']; }
At the body is the code that proccess the deleting.
<?php
if (isset($id)) {
$result = mysql_query ("DELETE FROM comments WHERE id='$id'");
if ($result == 'true') {echo "Comment Deleted!";}
else {echo "Error: Nothing was deleted!";}
} else {
echo "Unknown Error! Contact Administrator.";
}
?>
First step is to make your checkbox name an array.
And when your script receive the post, parse that array out to the appropriate sql clause.
Finally put the clause into the query in the where clause.