Uncaught exception 'PDOException'

I have an error: Uncaught exception ‘PDOException’ with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax

This happens when i want to update my form.
What have I done wrong??
Here is my PHP code…

//session already set in config file
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

include('config/db.php');



$id = $_GET['id'];

$sql = "SELECT * FROM stocks WHERE id=:id";

$query = $dbh -> prepare($sql);

$query -> execute([':id' => $id ]);

$result = $query ->fetch(PDO::FETCH_OBJ);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  
    $bloodbagid  = $_POST['bloodbagid'];
    $blood_type  = $_POST['blood_type'];
    $blood_volume  = $_POST['blood_volume'];
    $expiry_date  = $_POST['expiry_date'];
    $receive_date  = $_POST['receive_date'];
    $sent_date  = $_POST['sent_date'];

    $data = [':bloodbagid' => $bloodbagid,':blood_type' => $blood_type, ':blood_volume' => $blood_volume, ':expiry_date' => $expiry_date, ':receive_date' => $receive_date, ':sent_date' => $sent_date,':id' => $id,];

    $sql = "UPDATE stocks set bloodbagid=:bloodbagid, blood_type=:blood_type, blood_volume=:blood_volume, expiry_date=:expiry_date, receive_date=:receive_date, sent_date=:sent_date, WHERE id=:id";
    
    $query = $dbh -> prepare($sql);

    $query -> execute($data);
      
    header("location: view_stocks.php");
    
    exit();

  
}

?>

What’s that comma at the end of your list?

1 Like

Chorn is referencing the comma here, after sent_date, though the one at the end of your array two lines before is probably not good either.

1 Like

After removing the comma it has worked. But problem is when I want to edit Call Stack Error shows up.

This is my database config file:

session_start();
// DB credentials.

define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','bloodbank');
// Establish database connection.
try
{
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
exit("Error: " . $e->getMessage());
}
?>

feels like you’re including your db.php script more than once. See if that’s true somehow.

(One of your includes to this file is on line 53 of edit_stock.php ; look for another.)

1 Like

That’s true…I found it. It was careless of me. Thanks for the assist.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.