SQL Update query error in PHP function

I'm currently trying to use the following query for a PHP / SQL function with a page that edits medicines for a project that I am creating for a school. I get the following error and cannot find where the error is:

Syntax error or access violation: 1064 You have an error in the SQL syntax;

function edit_medicine($medicine_name, $medicine_dose, $medicine_date, $medicine_current, $medicine_id) {
global $db;
$query = "UPDATE Medicine
             SET MedicineName = :medicine_name, 
             Medicine Dose = :medicine_dose, 
             MedicineDatePrescribed = :medicine_date, 
             MedicineCurrent = :medicine_current
              WHERE MedicineKey = :medicine_id";
$statement = $db->prepare($query);
$statement->bindValue(':medicine_name', $medicine_name);
$statement->bindValue(':medicine_dose', $medicine_dose);
$statement->bindValue(':medicine_date', $medicine_date);
$statement->bindValue(':medicine_current', $medicine_current);
$statement->bindValue(':medicine_id', $medicine_id);
$statement->execute();
$statement->closeCursor();
}

I would be grateful for any help anyone can offer - this is the end of the final week, and I completely burned out. Thank!

+4
source share
2 answers

Use a quote in column names, especially in a column Medicine Dosebecause of its space ( ). Next time, do not use a space for the column names:

$query = "UPDATE `Medicine`
             SET `MedicineName` = :medicine_name, 
             `Medicine Dose` = :medicine_dose, 
             `MedicineDatePrescribed` = :medicine_date, 
             `MedicineCurrent` = :medicine_current
             WHERE `MedicineKey` = :medicine_id";
+4

, .

SELECT [Business Name],[Other Name] FROM your_Table

0

Source: https://habr.com/ru/post/1648594/


All Articles