Jump to content

[SOLVED] mysql INSERT and Dates


jeffrydell

Recommended Posts

I have a date in a variable - supposedly formatted properly for MySQL's date field type.  (YYYY-MM-DD).  When I attempt to insert a new record in a table, the date doesn't store properly.

 

$sql = "INSERT INTO `dogs` (`owner_id`, `callname`, `breed`, `sex`, `birthdate`, `height`, `accessid`, `accesstype`) VALUES ($oid, '$nm', '$brd', '$sex', '$dt2snd', '$ht', $aid, '$atyp')";

 

... always ends up with the birthdate field reading '0000-00-00', whether I put quotes on the $dt2snd or not.

 

Any suggestions on what to do or where to 'read up'?

Link to comment
https://forums.phpfreaks.com/topic/66664-solved-mysql-insert-and-dates/
Share on other sites

you need to define your date before you insert it

$date = date('Y-m-d, H-i-s');

 

It doesn't look like they are wanting that format. Do this instead:

$date = date("Y-m-d");

 

Show us where you give a value to your variable $dt2snd. Also, you need to have single quotes around all your variables in the query.

Plain & simple (to avoid any confusion), $dt2snd is a string.  The string is formatted as 2007-08-25.

 

If I explicitly include a fixed value in a MySQL INSERT statement, the value goes in to the record just fine, but when I reference a variable which is a string formatted exactly the same way, I get 0000-00-00 in the record.

 

The question is, do I need to do anything to a string which is formatted according to the MySQL date 'format' in order to be able to put that variable's value into the record being inserted?

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.