Click here to Skip to main content
16,021,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good morning everyone

I am trying to update the table with the new quantity selected, when I run the following function, however, I get this error:

<pre lang="Terminal">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 to update the quantity, 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"); 

}


?>


What I have tried:

I have tried to look for spelling error, but it doesn't seem to have any, I have been on this problem for two days, all the data is passed correctly.

What could be the issue here?

Thanks for your assistance.
Posted
Updated 14-Sep-21 17:07pm

1 solution

Looking at the code, you have only two parameters in your query but you set values for three. The column you're updating is not a parameter, it's the target for the update.

Have a try without the :orderedQuantity bind
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(':itemQuantity', $itemQuantity);
  $statement->bindValue(':soldID', $soldID);
  $result = $statement->execute();
  $statement->closeCursor();
  return $result;
}

?>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900