0

How can I remove multiple values (in an array) from MySQL Database? I have a string with User-ID and an array of Article_IDs.

$user_id = 1;
$article_id = [1,2,3,4];

$deleteValues = "DELETE FROM my_table WHERE user_id = ? AND id = ?";
$delt = $db->prepare($deleteValues);
$delt->bindParam(1, $user_id);
$delt->bindParam(2, $article_id);
$delt->execute();

I have different users and articles in the Database. I need activated Prepare Statements. My Example doesn't work.Thank you!

Dinuka Dayarathna
  • 169
  • 1
  • 3
  • 9
user6834389
  • 73
  • 1
  • 11
  • Have you checked that the parameter values actually match what you're expecting them to? And have you set/opened a connection properly? – David Dec 01 '16 at 12:43
  • Can you display the database and the public function you're running that code in? – Option Dec 01 '16 at 12:43
  • just to give you a hint, try to print the value of `$deleteValues` and then run the result directly in you MySQL console. – Dragos Dec 01 '16 at 12:43
  • 1
    You are trying to concatenate a string with an array when passing $article_id (an array) as parameter to that query. Try updating the query to `DELETE FROM my_table WHERE user_id = ? AND id IN(?)` – Dragos Dec 01 '16 at 12:45
  • 1
    Use the `IN` syntax `AND id IN ()` – RiggsFolly Dec 01 '16 at 12:45
  • I get Notice: Array to string conversion in – user6834389 Dec 01 '16 at 12:56
  • It works! $deleteValues = "DELETE FROM my_table WHERE user_id = ? AND id IN (" . implode(",", $articeValues) . ")"; $delt = $db->prepare($deleteValues); $delt->bindParam(1, $user_id); $delt->execute(); – user6834389 Dec 01 '16 at 13:07

0 Answers0