Jump to content

[SOLVED] help with multiple checkbox delete


justAnoob

Recommended Posts

Here is my form and the delete script... It echos that messages were deleted,,, but when I refresh the page and even check mysql database, everything is still there... Am I even close to getting this right?

 

Form.

<form id="form2" name="form2" method="post" action="deletemessages.php">
    <?php
$stone = $_SESSION['id'];
include "connection.php";
$sql = mysql_query("SELECT id, user_id, sendto, subject, message, datesent, status FROM member_messages WHERE sendto = '$stone'");

echo '<table width="950" border="0" align="center" cellpadding="0" cellspacing="0">';
while ($row = mysql_fetch_array($sql))
{
echo '<tr><td width="39">';
echo '<input type="checkbox" name="checkbox[]" id="checkbox[]" value="' . $row['id'] . '">';
echo '</td><td width="172">';
echo $row['user_id'];
echo '</td><td width="453">';
echo '<a href="readandreply.php?messid=' . $row['id'] . '">' . $row['subject'] . '</a>';
echo '</td><td width="175">';
echo $row['datesent'];
echo '</td><td width="115">';
echo '<font color="#006600">' . $row['status'] . '';
echo '</td></tr>';
}
echo "</table><br><br>";
echo '<input type="submit" name="submit" id="submit" value="Delete" />';
?>
    </form>

 

Delete script

<?php
include "connection.php";
$sql="SELECT * FROM member_messages";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

for($i=0;$i<$count;$i++)
{
  $del_id = $checkbox[$i];
  $sql = "DELETE FROM member_messages WHERE id = '$del_id'";
  $result = mysql_query($sql);
}

if($result)
{
  echo "Messages deleted.";
}

mysql_close();
?>

Link to comment
Share on other sites

your form uses the "POST" method, why aren't you calling any form values in your php script?  something like:

$rowid = $_POST['checkbox']; //assigns values to an array

 

To call these variables you would want to start with:

$checkbox[0]; //holds first value checked
$checkbox[1]; //holds second value checked and so on

 

I don't see how you are checking to see what the user actually selected for deletion.  Right now it looks like your php code goes through your list and deletes everything in there no matter what with id equal to $checkbox[$i].  However, it is evident that $checkbox[$i] is not holding a relevant value.  To troubleshoot this I would first echo that variable and see if there is anything even stored in it...I would assume nothing.  Your echoed confirmation will display no matter what because it does not rely on a conditional statement that confirms whether rows were deleted.

 

 

 

Link to comment
Share on other sites

$err  = 0;
$succ = 0;

if ( !empty($_POST['checkbox']) )
{

forEach ($_POST['checkbox'] as $id)
{
    if ( !ctype_digit($id) )
   {
      $err++;
   }
   else
   {
      $que = @mysql_query("DELETE FROM member_messages WHERE id = " . $id . " AND sendto = '" . $stone . "' LIMIT 1");

      if ($que)
      {
         $succ++;
      }
      else
      {
         $err++;
      }
   }
}
}
$msg = '';
if ($succ) $msg .= number_format($succ) . ' message(s) successfully deleted.<br ><br >';
if ($err) $msg  .= number_format($err) . ' message(s) could not be deleted.';

 

 

Link to comment
Share on other sites

Still having problems... I echoed row id next to each checkbox to make sure that each checkbox had a value, and they did. I get the success message of the checkbox items being deleted,, but nothing really gets deleted. Any other ideas?

<form id="form2" name="form2" method="post" action="deletemessages.php">
    <?php
$stone = $_SESSION['id'];
include "connection.php";
$sql = mysql_query("SELECT id, user_id, sendto, subject, message, datesent, status FROM member_messages WHERE sendto = '$stone'");

echo '<table width="950" border="0" align="center" cellpadding="0" cellspacing="0">';
while ($row = mysql_fetch_array($sql))
{
echo '<tr><td width="39">';
echo '<input type="checkbox" name="checkbox[]" id="checkbox[]" value="' . $row['id'] . '">';
echo '</td><td width="172">';
echo $row['user_id'];
echo '</td><td width="453">';
echo '<a href="readandreply.php?messid=' . $row['id'] . '">' . $row['subject'] . '</a>';
echo '</td><td width="175">';
echo $row['datesent'];
echo '</td><td width="115">';
echo '<font color="#006600">' . $row['status'] . '';
echo '</td></tr>';
}
echo "</table><br><br>";
echo '<input type="submit" name="submit" id="submit" value="Delete" />';
?>
    </form>

 

<?php
include "connection.php";
$sql="SELECT * FROM member_messages";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

for($i=0;$i<$count;$i++)
{
  $del_id = $checkbox[$i];
  $sql = "DELETE FROM member_messages WHERE id = '$del_id'";
  $result = mysql_query($sql);
}

if($result)
{
  echo "Messages deleted.";
}

mysql_close();
?>

Link to comment
Share on other sites

I don't want to be rude and I know others are busy helping others out, but is this something that is difficult to do? Or is this something small that I'm missing. I've been searching for a couple days on how to do this, and can't find anything anywhere.

Link to comment
Share on other sites

I almost got it,, I think...  Right now it deletes all the messages in the database(not good). I just want it to delete whatever checkboxes are selected. Look below, I'm not sure what to put in the WHERE clause.

<form id="form2" name="form2" method="post" action="deletemessages.php">
    <?php
$stone = $_SESSION['id'];
include "connection.php";
$sql = mysql_query("SELECT id, user_id, sendto, subject, message, datesent, status FROM member_messages WHERE sendto = '$stone'");

echo '<br><table width="950" border="0" align="center" cellpadding="0" cellspacing="0">';
while ($row = mysql_fetch_array($sql))
{
echo '<tr><td width="39">';
echo '<input type="checkbox" name="checkbox[]" value="' . $row['id'] . '">';
echo '</td><td width="172">';
echo $row['user_id'];
echo '</td><td width="453">';
echo '<a href="readandreply.php?messid=' . $row['id'] . '">' . $row['subject'] . '</a>';
echo '</td><td width="175">';
echo $row['datesent'];
echo '</td><td width="115">';
echo '<font color="#006600">' . $row['status'] . '';
echo '</td></tr>';
}
echo "</table><br><br>";
echo '  <input name="delete" type="submit" id="delete" value=" Delete " />';
?>
    </form>

<?php
include "connection.php";
$sql="SELECT * FROM member_messages";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

$checkbox = $_POST['checkbox']; 

if (count($checkbox) > 0)
{ 
   for ($i=0;$i<count($checkbox);$i++)
   { 
      $sql = "DELETE FROM member_messages WHERE id = '  WHAT TO PUT HERE  ' ";   // What should I put here?
      $result = mysql_query($sql);
   }
   if($result)
   {
      echo "Messages deleted.";
   }
}
mysql_close();
?>

Link to comment
Share on other sites

figured it out...

<?php
include "connection.php";
$sql="SELECT * FROM member_messages";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

$checkbox = $_POST['checkbox']; 

if (count($checkbox) > 0)
{ 
   for ($i=0;$i<count($checkbox);$i++)
   { 
      $del_id = $checkbox[$i];
  $sql = "DELETE FROM member_messages WHERE id = '$del_id' ";
      $result = mysql_query($sql);
   }
   if($result)
   {
      echo "Message(s) deleted.";
   }
}
mysql_close();
?>

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.