1

Doing the query manually in PHPMyAdmin

INSERT INTO productimages (ImageURL, productID)
VALUES('http://test.jpg', (SELECT id
                     FROM products 
                    WHERE products.MPN = 'test'));

Works just fine.

But trying to use PDO...

try {
   $sql = "INSERT INTO productimages (ImageURL, productID)
VALUES(':image_url', (SELECT id
                     FROM products 
                    WHERE products.MPN = ':mpn'));";
    $data = [
    'image_url' => $image_url,
    'mpn' => $mpn
];
    $stmt = $conn->prepare($sql);
    $stmt->execute($data);
    }
catch(PDOException $e)
    {
      echo '<h2 style="color:red;">' . $e->getMessage() . '</h2>';
    }

I am always getting this error:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

How do I properly form this INSERT query to perform with PDO?

Brian Bruman
  • 883
  • 12
  • 28

0 Answers0