PHP - error when insert date into MySQL

Posted by Michael Mao on Stack Overflow See other posts from Stack Overflow or by Michael Mao
Published on 2010-05-06T10:39:11Z Indexed on 2010/05/06 10:48 UTC
Read the original article Hit count: 237

Filed under:
|
|

Hello everyone:

I've got a typical problem when trying to insert a date into MySQL.

The column defined in MySQL is of type DATE. My PHP version is 5.3.0

Apart from this date-related issue, the rest of my code works just fine.

And this is my PHP script to do this:

$tablename = BOOKS_TABLE;
    $insert = mysql_query("INSERT INTO $tablename (barcode, book_name, volume_num,".
    " author, publisher, item_type, buy_price, buy_date) VALUES ".
    "(".
        "'" . $barcode      . "', ".
        "'" . $bookname     . "', ".
        "'" . $volumenum    . "', ".
        "'" . $author       . "', ".
        "'" . $publisher    . "', ".
        "'" . $itemtype     . "', ".
        "'" . $buyprice     . "', ".
        "'" . getMySQLDateString($buydate).
        //"'STR_TO_DATE('".$buydate ."', '%d/%m/%Y'))'". //nothing changes in MySQL
    ")");

And this is the faulty function :

function getMySQLDateString($buydate) //typical buydate : 04/21/2009
{
    $mysqlDateString = date('Y-m-d H:i:s', $strtotime($buydate)); 

    return $mysqlDateString;
}

The first commented out line wouldn't do anything, the script is executed with no error, however, there is nothing changed in datebase after this.

The current approach will cause a Fatal error saying function name must be a string in this line.

Actually I followed this thread on SO, but just cannot pass the date into MySQL...

Can anyone help me figure out which part is not right?

How would you do it, in this case, to get it right?

Sorry about such a journeyman-like question, thanks a lot in advance.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql