Search code examples
phpmysqlpdosql-updateprepared-statement

PDO prepared statement in UPDATE Multiple ID


I am Developing a simple shopping 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.


Solution

  • 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";
    $stmt = $conn->prepare("UPDATE `mshop_pms` SET `readperm` = :readperm WHERE `mid` = :mid");
    //now update and iterate
    foreach ($mid as $m) {
        $s->bindParam(':readperm', $isread);
        $s->bindParam(':mid', $m);
        $s->execute();
    }