PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi,
I have a little database, I can search for records and display them in an html table,
I would like to have a check box of every record returned by the search and a button that onclick will delete the selected records from the database.
I am far away from having any idea on where to start on this.
Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /usr/local/psa/home/vhosts/giustinianinicola.com/httpdocs/contacts/search.php on line 30
OK, that surely did something right,
now, the previous error is gone, and the delete button has appeared,
but when I select a record and click on delete , first the page refreshes and shows all record in the database and at the bottom I get the error :
Warning: Invalid argument supplied for foreach() in /usr/local/psa/home/vhosts/giustinianinicola.com/httpdocs/contacts/search.php on line 53
which would be line 26 in the example above.:
foreach ($_POST['delete'] as $val) {
which looks to me like the the POST variable named "delete" is not an array of things, but rather a single string with a value as "Delete". Your foreach() statement would need to reference the array of checkbox names/variables you used.
Hi, I must really apologize, but you just spoke a language that I have no idea what it means.
I keep looking at the code but I just don't get it.
I am sorry.
Here's some code I use for an inbox for members. It goes through and deletes each of the selected mail messages that the user has checked after they hit the delete button.
function deleteMail($_POST)
{
foreach($_POST as $key=>$value)
if (is_numeric($value))
{
//delete it from the database
mysql_query("DELETE FROM mail WHERE id='$value' WHERE toid='$this->id'")
or die ('cannot delete this piece of mail');
//delete it from their object
$this->inbox = deleteObjectFromArray($this->inbox, $value);
$this->newmail = 1;
}
return successmsg("message(s) deleted.");
}
Here's the code on the mail.php page where it displays the mail and the delete checkboxes:
Of course this uses objects but it follows the same idea. The user clicks on the checkboxes of the message they want to be deleted. Each checkbox has a value that's set to the ID of the message in the database. When the delete function is called it goes through each of the numeric values that were submitted and deletes them from the database for that particular member.
Note that if you used this on a form that submits any other kind of numeric values it would try to delete messages with and ID of that value and you could potentially delete fields you don't want to.