PDOStatement::execute(): SQLSTATE[HY093]: 参数编号无效:绑定变量的数量与第 27 行的标记数量不匹配
PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens on line 27
Hello! Stack overflow newbie here. I'm in the middle of this assignment and i got stuck on this part of it. I need to update the DB but im getting " Invalid parameter number: number of bound variables does not match number of tokens" error. I tried looking through the forum and although i can find some people with similar problems i still havent resolved it.
这是我的代码:
<?php
// Get the product data
$category_id = filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
$code = filter_input(INPUT_POST, 'code');
$name = filter_input(INPUT_POST, 'name');
$price = filter_input(INPUT_POST, 'price', FILTER_VALIDATE_FLOAT);
$product_id = filter_input(INPUT_POST, 'productID');
// Validate inputs
if ($category_id == null || $category_id == false ||
$code == null || $name == null || $price == null || $price == false) {
$error = "Invalid product data. Check all fields and try again.";
include('error.php');
} else {
require_once('database.php');
// Add the product to the database
$query = 'UPDATE products SET categoryID = :category_id, productCode = :code, productName = :name, listPrice = :price WHERE productID = :product_id';
// $db->exec($query);
$statement = $db->prepare($query);
$statement->bindValue(':category_id', $category_id, PDO::PARAM_INT);
$statement->bindValue(':code', $code, PDO::PARAM_INT);
$statement->bindValue(':name', $name, PDO::PARAM_INT);
$statement->bindValue(':price', $price, PDO::PARAM_INT); //line 27
$statement->execute();
$statement->closeCursor();
// Display the Product List page
include('index.php');
}
?>
在您的 SQL 中,您在 where 子句中使用了参数“:product_id”,但您似乎忘记了绑定它。添加绑定语句。
Hello! Stack overflow newbie here. I'm in the middle of this assignment and i got stuck on this part of it. I need to update the DB but im getting " Invalid parameter number: number of bound variables does not match number of tokens" error. I tried looking through the forum and although i can find some people with similar problems i still havent resolved it.
这是我的代码:
<?php
// Get the product data
$category_id = filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
$code = filter_input(INPUT_POST, 'code');
$name = filter_input(INPUT_POST, 'name');
$price = filter_input(INPUT_POST, 'price', FILTER_VALIDATE_FLOAT);
$product_id = filter_input(INPUT_POST, 'productID');
// Validate inputs
if ($category_id == null || $category_id == false ||
$code == null || $name == null || $price == null || $price == false) {
$error = "Invalid product data. Check all fields and try again.";
include('error.php');
} else {
require_once('database.php');
// Add the product to the database
$query = 'UPDATE products SET categoryID = :category_id, productCode = :code, productName = :name, listPrice = :price WHERE productID = :product_id';
// $db->exec($query);
$statement = $db->prepare($query);
$statement->bindValue(':category_id', $category_id, PDO::PARAM_INT);
$statement->bindValue(':code', $code, PDO::PARAM_INT);
$statement->bindValue(':name', $name, PDO::PARAM_INT);
$statement->bindValue(':price', $price, PDO::PARAM_INT); //line 27
$statement->execute();
$statement->closeCursor();
// Display the Product List page
include('index.php');
}
?>
在您的 SQL 中,您在 where 子句中使用了参数“:product_id”,但您似乎忘记了绑定它。添加绑定语句。