Page 1 of 1

very new to PHP trying to insert datetime into mysql

Posted: Wed Jan 06, 2010 8:41 pm
by kevinwinters
Hi,
I am using MySQL, PHP, Apache on a windows platform. I am tryint to insert the date into a MySQL table. I can not use the NOW() function because I need to add it via PHP, not the query.

My table field is set to datetime and I am entering it via this:

Code: Select all

(isset($_POST['soldselect'])?date("Y-m-d h:m:s"):null)  
When I look at the field, I see that it is: 0000-00-00 00:00:00

If I change the field to text and execute the code, I see that it is: 2010

I am more used to ASP and Access database where it is very easy to do this but can someone help me insert a date into the MySQL database? Thanks

Re: very new to PHP trying to insert datetime into mysql

Posted: Wed Jan 06, 2010 10:46 pm
by requinix

Code: Select all

date("Y-m-d H:i:s")
will work as long as you put quotes around it in the query, like

Code: Select all

$query = "...'" . date("Y-m-d H:i:s") . "'..."
For NULL (assuming the field allows them) you'd not use quotes.

Code: Select all

$query = "...NULL..."

Re: very new to PHP trying to insert datetime into mysql

Posted: Wed Jan 06, 2010 11:24 pm
by kevinwinters
Probably should have posted more code. I solved the issue by changing the date_sold format from %d to %s in the select statement (I guess because it needed to be formatted as a string?)

Code: Select all

 
 
$jdate = date("Y-m-d h:m:s");
 
sprintf("
        UPDATE inventory SET
            name = '%s',
            price = %01.2f,
            sales_status = '%s',
            date_last_modified = NOW(),
            date_sold = '%s'
        WHERE
            id = %d
        ",
        Clean($_POST['p_name']),
        Clean($_POST['price']),
        Clean($_POST['sales_status']),
        (isset($_POST['soldselect'])?"$jdate":null),
        Clean($_POST['id'])
        )