very new to PHP trying to insert datetime into mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kevinwinters
Forum Newbie
Posts: 20
Joined: Wed Jan 06, 2010 8:31 pm

very new to PHP trying to insert datetime into mysql

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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..."
kevinwinters
Forum Newbie
Posts: 20
Joined: Wed Jan 06, 2010 8:31 pm

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

Post 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'])
        )
 
 
Post Reply