在PHPMyAdmin中手动进行查询
INSERT INTO productimages (ImageURL, productID)
VALUES('http://test.jpg', (SELECT id
FROM products
WHERE products.MPN = 'test'));
工作正常。
但是尝试使用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>';
}
我总是收到此错误:
SQLSTATE [HY093]:参数编号无效:绑定变量的数量与令牌的数量不匹配
如何正确构成此INSERT查询以与PDO一起执行?