views:

14

answers:

0

Hi! I'm just learning to use PDO, and I want to bind params to a delete statement using a loop. I'm using a code like this that definitely doesn't works:

public function delete_user ($post, $table='mytable') {
$delete = $this->conn->prepare("DELETE FROM $table WHERE id IN (:id) ");

  // Works but only delete the last element passed by the "for" loop
    /*for ($i = 0; $i < count($post); $i++) { 
    $delete->bindParam(':id', $post[$i], PDO::PARAM_INT);  
        }*/

    // Works but only delete the last element passed by the foreach
/*foreach ($post as $ids) {
    $delete->bindParam(':id', $ids, PDO::PARAM_INT);
    }*/

   $delete->execute();    
   return ($delete->rowCount() !=1) ? false : true;

}

1) I have tested to introduce the $_POST as an array, and then making a foreach to get the ids in order to add them as bindParam in the statement.

2) I have tested too, to give to the function a string as result of make the following: $post = implode(',', $_POST['checkboxes']);

None of them works. Well, both works, but only delete the last result passed by for loop or foreach.

Now, I'm, using this that works:

public function delete_user ($post, $table='mytable') { $delete = $this->conn->prepare("DELETE FROM $table WHERE id IN (".$post.")"); $delete->execute();
return ($delete->rowCount() !=1) ? false : true; } But I'd really like to use bindParm if it's possible.

¿How can I get this working?

Thanks and sorry for my english!