SQL 查询更新商品数量

问题描述 投票:0回答:1

我正在尝试使用所选的新数量更新表格,但是当我运行以下函数时,出现此错误:

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\php_Assessments\shoppingList\model\functions_products.php:11 Stack trace: #0 C:\xampp\htdocs\php_Assessments\shoppingList\model\functions_products.php(11): PDOStatement->execute() #1 C:\xampp\htdocs\php_Assessments\shoppingList\controller\product_update_process.php(21): update_item('57', '3', '1') #2 {main} thrown in C:\xampp\htdocs\php_Assessments\shoppingList\model\functions_products.ph

更新数量的函数,function_products.php:

<?php
function update_item($soldID, $orderedQuantity, $itemQuantity)
{
  global $conn;
  $sql = "UPDATE shopping_items.sold SET orderedQuantity = :itemQuantity WHERE soldID = :soldID";
  $statement = $conn->prepare($sql);
  $statement->bindValue(':soldID', $soldID);
  $statement->bindValue(':orderedQuantity', $orderedQuantity);
  $statement->bindValue(':itemQuantity', $itemQuantity);
  $result = $statement->execute();
  $statement->closeCursor();
  return $result;
}

?>

product_update_process.php

<?php
// Require database connection
require('connection.php');
// Require function
require_once("../model/functions_products.php");
// Fetch the data required
$soldID = $_GET['soldID'];
$itemQuantity = $_POST['itemQuantity'];
$orderedQuantity = $_POST['orderedQuantity'];
if(empty($itemQuantity)) {
   echo '<script type="text/javascript">alert("The quantity is required.")</script>' ;
   // Redirect the browser window back to the add customer page
    echo "<script>setTimeout(\"location.href = '../index.php';\",2000);</script>";
} else {
    //call the update_item() function
    $result = update_item($soldID, $itemQuantity, $orderedQuantity);
    // Redirect the browser window back to the admin page
    header("location: ../index.php"); 

}


?>

这里可能出现什么问题?

php pdo
1个回答
1
投票

要添加@TangentiallyPerpendillary 的评论,为什么要绑定到

:orderedQuantity
? 尽管您已经告诉 SQL 引擎需要该变量,但您的 SQL 语句中并未使用该变量。 该列不需要是变量才能将变量传递给它。

© www.soinside.com 2019 - 2024. All rights reserved.