Jump to content

Deleting rows in MySQL databse using checkbox NOT working! Help!


angelali

Recommended Posts

I want to delete rows in a table of my database using check-box.

Here are my codes below:

 

<?php
$con = mysql_connect('localhost', 'root', '') or die ('Connection Failed');
mysql_select_db('img', $con) or die ('Connection Failed');
$display = mysql_query("SELECT * FROM photos WHERE email='$lemail'");
echo '<input type="submit" value="Delete" name="del"/>';
echo "<table>
<tr>
<th>#</th>
<th>Images</th>
<th>Image description</th>
<th>Delete</th>
</tr>";
while($row = mysql_fetch_array($display))
{
echo "<tr>";
echo "<td>".$row['img_ID']."</td>";
  echo "<td><img src='folder/".$row['imaged']."' alt='alt text' width='100' height='100' class='thumb'/> </td>";
echo "<td>".$row['image_description']."</td>";
echo '<td><input type="checkbox" name="delete[]" value="'.$row['img_ID'].'"/></td>';
echo "</tr>";
}
echo "</table>";
echo "</form>";
if (isset($_POST['delete'])) {
$del = $row['img_ID'];
for($i=0;$i<count($_POST["delete"]);$i++)
{
if($_POST["delete"][$i] != "")
{
$str = "DELETE FROM photos WHERE img_ID='$del' ";
mysql_query($str);
echo "Record Deleted.";
}
} 
}
mysql_close($connect);
?> 

 

Here are the problems:

 

1/ Not working at all, I mean no image is being deleted

2/ At then end, I display a message that the record has been deleted, but if I check multiple checkbox, it keeps writing the message "Records deleted" multiple times

 

My images are stored in a folder while its details in database...

 

Help, thank you  :shy:

Link to comment
Share on other sites

The message will display several times because it is within a loop. So if you delete 5 images, the message will be displayed 5 times. Rather than executing the query within a loop (a very bad idea) you should use something like:

 

 

if (isset($_POST['delete'])) {
    foreach($_POST['delete'] as $id => $val)
    {
        $ids[] = $id;
    }
    mysql_query("DELETE FROM photos WHERE img_ID IN (".implode(',',$ids).")");
    echo "Record Deleted.";
}


 

 

This assumes the checkboxes have the name "delete[$row['img_ID']]".

The foreach loop will loop through them all and add the img_ID to an array($ids)

The query will then delete all images where the img_ID exists in the array($ids).

When they query is finished it will echo your message only once.

 

 

also this wont work:

$del = $row['img_ID'];

 

 

its outside the variable scope of the loop it is used in ($row).

Link to comment
Share on other sites

Still not deleting, but it does give me the message it has been deleted. Is something wrong in the codes?

 

 

<?php
$con = mysql_connect('localhost', 'root', '') or die ('Connection Failed');
mysql_select_db('img', $con) or die ('Connection Failed');
$display = mysql_query("SELECT * FROM photos WHERE email='$lemail'");
echo '<input type="submit" value="Delete" name="del"/>';
echo "<table>
<tr>
<th>#</th>
<th>Images</th>
<th>Image description</th>
<th>Delete</th>
</tr>";
while($row = mysql_fetch_array($display))
{
echo "<tr>";
echo "<td>".$row['img_ID']."</td>";
  echo "<td><img src='folder/".$row['imaged']."' alt='alt text' width='100' height='100' class='thumb'/> </td>";
echo "<td>".$row['image_description']."</td>";
echo '<td><input type="checkbox" name="delete[]" value="'.$row['img_ID'].'"/></td>';
echo "</tr>";
}
echo "</table>";
echo "</form>";
if (isset($_POST['delete'])) {
    foreach($_POST['delete'] as $id => $val)
    {
        $ids[] = $id;
    }
    mysql_query("DELETE FROM photos WHERE img_ID IN (".implode(',',$ids).")");
    echo "Record Deleted.";
}
mysql_close($connect);
?> 

 

 

 

Link to comment
Share on other sites

Well, I successfully did it well...thank you guys.. however  to problems remain:

 

1/ How can I tell the user that the checkbox must be checked if he clicked on the delete button without checking? Because, i used POST here, and I think 'empty()' or ' !="" ' will not work! I tried '!isset' also.

 

2/ I tried implementing mysql_real_escape_string and strip_tags to the checkbox, but gives me an error as it is conflicting with the FOR Each loop

Link to comment
Share on other sites

I think this would work?

if (isset($_POST['delete'])) {
  // mysql delete code here...
}
else
{
  echo "Select a checkbox!";
}

 

Print_r doesn't show anything because your condition is testing for the existence of $_POST['delete']. If it doesn't exist, print_r isn't called at all!

Link to comment
Share on other sites

Well, I tried that before, I even tries with !isset, but it writes the message which to display to the user that a checkbox should be checked before clicking on the delete button.. If this is not a bad principle or whatever, I will forget it, however, the mysql_real_escape_string and the strip_tags are more important.... I tried, but gives me an error.... Just tell me where to implement them thats all..

 

For example, I tried this:

 

if (isset($_POST['delete'])) {.......

$dimg = mysql_real_escape_string(strip_tags($_POST['delete']));

 

But it gave me an error, which I forgot as I already removed it...

Link to comment
Share on other sites

Let's forget the isset for some minutes, what's more important is to protect the checkbox from attacks, I tried the following methods to protect the checkbox from attacks:

 

if (isset($_POST['delete'])) {
$del_img = mysql_real_escape_string(strip_tags($_POST['delete']));
foreach($del_img as $id => $val)
{
$ids[] = $val;

}
mysql_query("DELETE FROM photos WHERE img_ID IN (".implode(',',$ids).")");
echo "Record Deleted.";
}

 

I got these errors:

 

Warning: strip_tags() expects parameter 1 to be string

 

Warning: Invalid argument supplied for foreach()

 

Notice: Undefined variable: ids

 

Warning: implode() [function.implode]: Invalid arguments passed

Link to comment
Share on other sites

this should help solve most of those errors

 

 

<?php
$del_img = (string) $_POST['delete'];
if (isset($del_img)) {
$delete = mysql_real_escape_string(strip_tags($del_img));
$deleted = str_split($delete);
foreach($deleted as $id)
{
$ids[] = implode(',',$id);

}
mysql_query("DELETE FROM photos WHERE img_ID IN ($ids)");
echo "Record Deleted.";
}

?>

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.