2

I'm trying to insert the datetime into a mysql column but I'm getting this error:

Error: INSERT INTO prices (priceLBTC, dt) VALUES (421.59, 2015-12-27 15:41:09) 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 '15:41:09)' at line 2

Here is the code I'm using to get the datetime and insert it into the "dt" datetime type column.

$dateTime = new DateTime();
$date = $dateTime->format('Y-m-d H:i:s');

$sql = "INSERT INTO prices (priceLBTC, dt)
VALUES ($bitcoinPrice, $date)";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
1
  • 2
    Put quotes around $date. Alternatively, consider using timestamps Commented Dec 27, 2015 at 21:15

2 Answers 2

3

You are getting error cause your datetime value is not quoted as seen below and so DB engine throwing an error assuming you meant to provide more values.

INSERT INTO prices (priceLBTC, dt) VALUES (421.59, 2015-12-27 15:41:09)
                                                   ^.. Here

You need to quote your datetime value using single quote like

INSERT INTO prices (priceLBTC, dt) VALUES (421.59, '2015-12-27 15:41:09')
Sign up to request clarification or add additional context in comments.

1 Comment

That's it, thanks! I didn't think it needed any because the price had none originally.
1

$sql = "INSERT INTO prices (priceLBTC, dt) VALUES (".$conn->quote($bitcoinPrice).", ".$conn->quote($date).")";

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.