3

I have this code,

$q = $dbc -> prepare ("SELECT * FROM tasks ORDER BY date_time LIMIT 0, 15");
$q -> execute();

echo '<form action="/adminpanel?tab=process" method="post">
          <input type="hidden" name="deletetask" />';

while ($todo = $q -> fetch(PDO::FETCH_ASSOC)) {

  echo '<div id="update"><div><strong>' 
           . $todo['date_time'] . '</strong><span>' . $todo['type'] 
           . '</span></div><p>' . $todo['message'] 
           . '</p><input class="checkbox" name="deletetask" value="' 
           . $todo['date_time'] . '" type="checkbox" /></div>';
}

echo '<input type="submit" value="Delete Tasks" /></form>';

Now everything works as expected apart from one thing and I haven't really found any answers on the internet. My while loop will have always more than one row, and will almost always want more than one deleting from it to.

As this script stands the form does work but it will only delete the last checkbox that was clicked. I understand why it is doing this, but I don't understand how to overcome this problem I am using PDO and prepared statements.

7
  • I have not seen any code that is used to delete rows. Commented Aug 29, 2011 at 8:24
  • The code is ("DELETE FROM table WHERE id = ?"); Commented Aug 29, 2011 at 8:26
  • The code shoud be DELETE FROM table WHERE id IN(?,?,?...?) Commented Aug 29, 2011 at 8:27
  • Well - change it! Debug it, rewrite it, decompose it. Are you programmer or what? Commented Aug 29, 2011 at 8:39
  • 1
    @xdazz I didn't copy and paste it, I didn't even try it, I knew it would not work as my form values are displayed in a while loop making the name the same, so in php the $_POST variable would get over ridden with the new value. Commented Aug 29, 2011 at 8:54

2 Answers 2

3

You are assigning the same name="deletetask" for every checkbox. So, when you submit your form, you receive only last selected deletetask value. So, your mistake is here

<input class="checkbox" name="deletetask" value=

Should be

<input class="checkbox" name="deletetask[]" value=

So you need to rename deletetask to deletetask[] so your checkboxes is sent as an array and than do something like

$todelete = $_POST['deletetask']; 
//or $_GET, if you are submitting form through get. But I would recommend you using POST
$stmt = $pdo->prepare("DELETE FROM table WHERE id = ?");
foreach ($todelete as $id)
    $stmt->execute($id);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you will try this now :) finally someone who understood my comment.
1

Here is a simple way to do multiple deletes from your database:

HTML

<input type="checkbox" name="del[]" value="your id">
<input type="checkbox" name="del[]" value="your id">
<input type="checkbox" name="del[]" value="your id">
<input type="checkbox" name="del[]" value="your id">

PHP

$ids = array();
foreach ($_POST['del'] as $pval) {
  $ids[] = (int)$pval;
}
$ids = implode(',',$ids);
$query = $db->prepare("DELETE FROM `pages` WHERE `pages_id` IN ( $ids )");
$query->execute();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.