PDO插入,带有选择语句错误/无效的参数号

时间:2018-11-18 22:58:20

标签: php mysql pdo

在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一起执行?

0 个答案:

没有答案