Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello! I'm making a simple table for a school project and I've found this tutorial helpful
"https://www.youtube.com/watch?v=NqP0-UkIQS4">
PHP and MySQL with CRUD Operations: Create, Read, Update, Delete - YouTube
I followed everything but when it comes to updating/editing my table it always results into:

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= `2023-03-22T11:50:28`, Number_Of_Items = `5`, Item = `Tempra`, Expiration_D...' at line 1 in C:\xampp\htdocs\Inven\edit.php:75 Stack trace: #0 C:\xampp\htdocs\Inven\edit.php(75): mysqli_query(Object(mysqli), 'UPDATE clinic_i...') #1 {main} thrown in C:\xampp\htdocs\Inven\edit.php on line 75.

Sorry for the bad english, not my first language and thank you.

What I have tried:

query($sql);
	$row = $result->fetch_assoc();

	if (!$row) {
		header("location: /inven/index.php");
		exit;
	}


$Date_Received = $row["Date_Received"];
$Number_Of_Items = $row["Number_Of_Items"];
$Item = $row["Item"];
$Expiration_Date = $row["Expiration_Date"];
$Last_Updated = $row["Last_Updated"];
$Quantity = $row["Quantity"];

}
	else{

$id = $_POST["id"];
$Date_Received = $_POST["Date_Received"];
$Number_Of_Items = $_POST["Number_Of_Items"];
$Item = $_POST["Item"];
$Expiration_Date = $_POST["Expiration_Date"];
$Last_Updated = $_POST["Last_Updated"];
$Quantity = $_POST["Quantity"];

	do {
		if ( empty($id) || 
			empty($Date_Received) || 
			empty($Number_Of_Items) || 
			empty($Item) || 
			empty($Expiration_Date) || 
			empty($Last_Updated) || 
			empty($Quantity) ) 
			{ $errorMessage= "All the Fields are required";
				break;
		} 

		$sql = "UPDATE clinic_inventory" .
			"SET Date_Received = `$Date_Received`, Number_Of_Items = `$Number_Of_Items`, Item = `$Item`, Expiration_Date = `$Expiration_Date`, Last_Updated = `$Last_Updated`, Quantity = `$Quantity`" . "WHERE id = $id";
		
			$result = mysqli_query($connection, $sql);

			if (!$result) {
			$errorMessage = "Invalid Query: " . $connection->error;
			break;
		}

			$successMessage = "Item Successfully Added";
			header("location: /inven/index.php");
			exit;
			
}	while (false);
	
}


?>
Posted

1 solution

In SQL, single quotes should be used for string values, not backticks. Also, there's a missing space before the 'SET' in your query which reads as - "UPDATE clinic_inventorySET. Your sql should look like -

PHP
$sql = "UPDATE clinic_inventory SET " .
       "Date_Received = '$Date_Received', " .
       "Number_Of_Items = '$Number_Of_Items', " .
       "Item = '$Item', " .
       "Expiration_Date = '$Expiration_Date', " .
       "Last_Updated = '$Last_Updated', " .
       "Quantity = '$Quantity' " .
       "WHERE id = $id";


You are also leaving yourself wide open to SQL injection, you should rather use prepared statements and parameterized queries - Prepared statements and stored procedures[^]

Your code should, as a rough estimate look like this -
PHP
$sql = "UPDATE clinic_inventory SET " .
       "Date_Received = ?, " .
       "Number_Of_Items = ?, " .
       "Item = ?, " .
       "Expiration_Date = ?, " .
       "Last_Updated = ?, " .
       "Quantity = ? " .
       "WHERE id = ?";

$stmt = $connection->prepare($sql);

if ($stmt) {
    $stmt->bind_param("ssssssi", $Date_Received, $Number_Of_Items, $Item, $Expiration_Date, $Last_Updated, $Quantity, $id);

    $stmt->execute();

    if ($stmt->affected_rows > 0) {
        //Your query was successful...
        $successMessage = "Item Successfully Added";
        header("location: /inven/index.php");
        exit;
    } else {
        //Handle error/s...
        $errorMessage = "Error updating item: " . $stmt->error;
    }

    $stmt->close();
} else {
    //Handle error/s...
    $errorMessage = "Error preparing statement: " . $connection->error;
}
 
Share this answer
 
v3
Comments
Raiden Peñaverde 11-Mar-24 19:20pm    
Thank you!!
Andre Oosthuizen 12-Mar-24 12:09pm    
You're welcome.

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