Jump to content

PHP/MySQL date syntax problems.


timlondon

Recommended Posts

I'm having a problem using PHP to insert a date into a MySQL field.

The field is already defined as a date data type.

The offending PHP code is:

$sql = "INSERT INTO user (maindob) values ('$newdata[1]') WHERE account = '$newdata[0]'";
echo $sql;

$sql therefore equals:
INSERT INTO user (maindob) values (1966-08-26) WHERE account = 'alpha01'

But I'm getting the following error:

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 'WHERE account = 'alpha01'' at line 1

Any advice would be much appreciated.
Link to comment
https://forums.phpfreaks.com/topic/31105-phpmysql-date-syntax-problems/
Share on other sites

Because you don't have your date within quotes, MySQL is attempting to run an arithmetic operation on your date (1966 minus 08 minus 26). You need to simply place single quotes around the date, and you should be fine:
[code]
<?php
$sql = "INSERT INTO user (maindob) VALUES ('$newdata[1]')";
?>
[/code]

Good luck

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.