mehdi mehdi - 6 months ago 21
MySQL Question

PDO prepared statement in UPDATE Multiple ID

I am Developing a simple shooping system ,and using

PDO
.
But I can't seem to use PDO while updating using multiple values ...
Look here :

// GET MESSAGES ID AND REPLACE '-' WITH ','
$mid = explode("," , str_replace( '-' , ',' , $mid ));
$isread = "read";

$stmt = $conn->prepare("UPDATE `mshop_pms` SET `readperm` = ? WHERE `mid` IN (?)");
$stmt->execute(array($isread, array($mid)));


How can I do this? and do this :

$stmt->execute(array($isread, $mid));


Without explode It just Update first row.

Thanks in advance.

Answer

It won't work in this way. You need to iterate over the exploded array, then keep on updating.

Here's an example :-

// GET MESSAGES ID AND REPLACE '-' WITH ','
    $mid = explode("," , str_replace( '-' , ',' , $mid ));
    $isread = "read";
try
{
$stmt = $conn->prepare("UPDATE `mshop_pms` SET `readperm` = :readperm WHERE `mid` = :mid");
}
catch(PDOException $e)
{
echo $e->getMessage();
}
    //now update and iterate
foreach($mid as $m)
{
$s->bindParam(':readperm', $isread);
$s->bindParam(':mid', $m);
$s->execute();
}