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
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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.