I am having a file which displays records from a database in a table, each row with a checkbox. when the checkbox is selected then that record is deleted. Now I can only delete one record a the time. I would like some comment on how to pass to multiple values to the delete.php file. I tried with
foreach ($_POST['id' ] as $Id), but I only got one value passed.
This is the file which displays the data:
<?php
$db = mysql_connect("localhost","user","password")
or die("Could not connect : ". mysql_error());
mysql_select_db("databasename", $db);
$query = "select * from test order by Id";
$result = mysql_query ($query,$db)
or die ("Query failed : ". mysql_error());
//Printing Results in Html
print "<html><head><title>database test</title></head><body bgcolor=\"lightblue\">\n";
print "<span style=\"position:absolute; top:200px;left:200;\">\n";
print "<form action=\"delete.php\" method=\"post\">\n";
print "<table border=\"1\" bordercolor=\"black\" bgcolor=\"white\">\n";
print "<tr bgcolor=\"lightblue\">
<td>Id</td><td>Voornaam</td><td>Achternaam</td>
</tr>\n";
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td>
<td bgcolor=\"lightblue\">
<input type=\"checkbox\" name=\"id\" value=\"$row[0]\">
</td>
</tr>");
}
//print "<tr><td>Id</td><td><input type=\"text\" name=\"Id\" /></td></tr>";
print "</table>\n";
print "<input type=\"submit\" value=\"delete\">\n";
print "</form>\n";
print "</span>\n";
require ('form.html');
print "</body></html>";
mysql_free_result($result); // free resultset
mysql_close($db); //close //close connection
//<input type=\"hidden\" name=\"Id\" value=\"$row[0]\" >
//<input type=\"checkbox\" check=\"$row[0]\"/>
?>
And this is the file which i use to delete records:
<?php
//maakt variabelen uit formulier toegankelijk
$Id = $_POST['id'];
echo "$Id";
echo "<br /><br />";
//database verbinding
$db = mysql_connect("localhost","user","password")
or die("Could not connect : ". mysql_error());
// query uitvoeren
mysql_select_db("databasename", $db);
$query = "delete from test where Id = '$Id' ";
$result = mysql_query ($query,$db)
or die ("Query failed : ". mysql_error());
echo "Thank you, records were succesfully deleted!<br />";
// redirect naar een andere pagina
print "Click <a href=\"http://link">here</a> to go back to the page";
mysql_free_result($result);
// free resultset
mysql_close($db);
?>
Thanks,
Jonathan