Jump to content

Deleting database row using checkbox


perky416

Recommended Posts

Hi Everyone,

 

Im trying to add a delete button to the message inbox page of my website, where when delete is clicked, the checked messages get deleted from the database.

After a bit of research on google i have come up with the code below however i cant get it to work. I have replaced the mysql delete query with echo "checked"; just for debugging.

 

$query = mysql_query("SELECT * FROM users WHERE id='$id'");
$row = mysql_fetch_assoc($query);

$submit = $_POST['submit'];
$username = $row['username'];
$checked = $_POST['message'];

$messages_query = mysql_query("SELECT * FROM messages WHERE recipient='$username'");
if (mysql_num_rows($messages_query) > 0)
{
   while ($messages_row = mysql_fetch_array($messages_query))
   {	
   		if ($messages_row['message_read'] == 0) { echo "<div style='background-color:#FFCCCC;'>"; }
	$message_id = $messages_row['id'];
   		echo "<a href='message.php?id=$message_id'>";
	echo "From: " . $messages_row['sender'];
	echo "Subject: " . $messages_row['subject'] . "<br />";
	echo "</a>";
	echo "<form method='POST'><input type='checkbox' name='message[]' value='1' /></form>";
	if ($messages_row['message_read'] == 0) { echo "</div>"; }
   }
}
else
{
   echo "No messages";
}

if ($submit)
{
if ($checked == "1")
{
	echo "checked";
}
}
?>
<html>
<form method="POST">
<input type="submit" name="submit" value="Delete" />
</form>
</html>

 

I have also tried the following but i still cant get it to work:

 

if (isset($checked))
{
	echo "checked";
}

 

Can anybody see where iv gone wrong?

 

Thanks

Link to comment
Share on other sites

Hi Pikachu

 

Does this look better:

 

$query = mysql_query("SELECT * FROM users WHERE id='$id'");
$row = mysql_fetch_assoc($query);

$submit = $_POST['submit'];
$username = $row['username'];
$checked = $_POST['message'];

$messages_query = mysql_query("SELECT * FROM messages WHERE recipient='$username'");
if (mysql_num_rows($messages_query) > 0)
{
   while ($messages_row = mysql_fetch_array($messages_query))
   {	
   		if ($messages_row['message_read'] == 0) { echo "<div style='background-color:#FFCCCC;'>"; }
	$message_id = $messages_row['id'];
   		echo "<a href='message.php?id=$message_id'>";
	echo "From: " . $messages_row['sender'];
	echo "Subject: " . $messages_row['subject'] . "<br />";
	echo "</a>";
	echo "<form method='POST'><input type='checkbox' name='message[]' value='1' />";
	if ($messages_row['message_read'] == 0) { echo "</div>"; }
   }
}
else
{
   echo "No messages";
}

if ($submit)
{
if ($checked == "1")
{
	echo "checked";
}
}
?>
<html>
<input type="submit" name="submit" value="Delete" />
</form>
</html>

 

I can get the echo "checked"; to display when i change the <input type='checkbox' name='message[]'  to <input type='checkbox' name='message', however with name='message[]' its just not working, its racking my brain iv been trying to figure out what is wrong all afternoon.

 

Thanks

Link to comment
Share on other sites

Iv been having a play but i still cant get it. When i use the following i can successfully echo the id of the message that has been checked.

 

$query = mysql_query("SELECT * FROM users WHERE id='$id'");
$row = mysql_fetch_assoc($query);

$submit = $_POST['submit'];
$username = $row['username'];
$checked = $_POST['message'];
$checkname = $_POST['checkname'];

$messages_query = mysql_query("SELECT * FROM messages WHERE recipient='$username'");
if (mysql_num_rows($messages_query) > 0)
{
   while ($messages_row = mysql_fetch_array($messages_query))
   {	
   		if ($messages_row['message_read'] == 0) { echo "<div style='background-color:#FFCCCC;'>"; }
	$message_id = $messages_row['id'];
   		echo "<a href='message.php?id=$message_id'>";
	echo "From: " . $messages_row['sender'];
	echo "Subject: " . $messages_row['subject'] . "<br />";
	echo "</a>";
	echo "<form method='POST'><input type='checkbox' name='checkname[]' value='$message_id' />";
	if ($messages_row['message_read'] == 0) { echo "</div>"; }
   }
}
else
{
   echo "No messages";
}

if ($submit)
{
if(isset($checkname))
{
	echo implode($checkname);
}
}

?>
<html>
<input type="submit" name="submit" value="Delete" />
</form>
</html>

 

However when i change the echo to mysql_query("DELETE FROM messages WHERE id='$message_id'"); and click delete, the message with the latest id gets deleted.

 

Any ideas?

 

Thanks

 

Link to comment
Share on other sites

Move your FORM open tag outside of the WHILE loop.

 

if (mysql_num_rows($messages_query) > 0)
{
   echo "<form method='POST'>";
   while ($messages_row = mysql_fetch_array($messages_query))
   {

   	if ($messages_row['message_read'] == 0) { echo "<div style='background-color:#FFCCCC;'>"; }
$message_id = $messages_row['id'];
   	echo "<a href='message.php?id=$message_id'>";
echo "From: " . $messages_row['sender'];
echo "Subject: " . $messages_row['subject'] . "<br />";
echo "</a>";
echo "<input type='checkbox' name='message[]' value='1' />";
if ($messages_row['message_read'] == 0) { echo "</div>"; }
   }
}

 

But with this code you cannot tell WHICH checkbox was checked. Change the CHECKBOX line to :

	echo "<input type='checkbox' name='message[]' value='$message_id' />";

 

Then you can walk the array of checkboxes with something like this:

psuedo-code

foreach ($_POST['message'] as $msgID) {
  DELETE FROM messages WHERE id = $msgID;
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.