4

I'm currently trying to use the following PHP function/SQL query with a page that edits medications on a project I'm building for school. I'm getting the following error and having trouble to finding where the error is :

Syntax error or access violation: 1064 You have an error in your 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'd appreciate any help anyone can offer- it's the end of finals week and I'm totally burnt out. Thanks!

2
  • Thanks for your honesty Commented Jul 21, 2016 at 2:23
  • ``Medicine Dose` , not 'Medicine Dose', please refer to Logan s answer Commented Jul 21, 2016 at 3:52

2 Answers 2

4

Use quote to your column names, especially your Medicine Dose column because of its space (). Next time, don't use space to name your columns:

$query = "UPDATE `Medicine`
             SET `MedicineName` = :medicine_name, 
             `Medicine Dose` = :medicine_dose, 
             `MedicineDatePrescribed` = :medicine_date, 
             `MedicineCurrent` = :medicine_current
             WHERE `MedicineKey` = :medicine_id";
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! However, that didn't fix my query. I appreciate your answer, though. I'm still getting "Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''MedicineName' = 'Topamax', 'Medicine Dose' = '20 mg', ' at line 2' "
You need to alter table and change column name to something like MedicineDose or Medicine_Dose
@SarahB - are you sure you used backticks (`), not apostrophe/single tick ('). Because they are different to each other.
0

If double quotes or backtick does not work , try including the string within square brackets.

For example

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.