tags:

views:

281

answers:

4

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.

Updated:

Thanks for reminding me this, and here is the exact error message as the html output :

Fatal error: Function name must be a string in C:\xampp\htdocs\comic\app\insertBookIntoDB.php on line 85

which point to the line starts with

$mysqlDateString = date('Y-m-d H:i:s', $strtotime($buydate));
A: 

In my opinion, storing dates using explicit date formats is annoying, and all the string conversion irritates me. I'd recommend using a BIGINT for the date field, and storing an epoch time value in the database.

You can get epoch time with PHP:

time();

Or, with more precision:

microtime();

Plus, you can manipulate them easily. To change the date to next day:

$myTime + 86400;

This epoch time format is very easy to store and use and isn't bugged with all the string conversion nonsense. To get a neat string from the value:

date("FORMAT STRING", $myTime);
Delan Azabani
@Delan Azabani : Actually the $buydate comes from the browser which is genreated by a javascript datepicker widget, which will fit the way aussies do with writing a date (DD/MM/YYYY). It is part of the "rules" from my client :)
Michael Mao
I wrote a script that automatically replaces text fields (that hold epoch timestamps) with user-friendly dropdown-based inputs (just include the script). Even when the input is friendly, the epoch timestamp still gets sent: http://azabani.com/43
Delan Azabani
@Delan Azabani : This is the jquery plugin I am using for getting the "correct" way of writing dates in Australia (DD/MM/YYYY) :http://www.eyecon.ro/datepicker/, which is really good :)
Michael Mao
If you choose to keep using that, then just send a different value to the server, modified with JavaScript: `Math.floor((new Date(picker.value)).valueOf()/1000)`
Delan Azabani
+1  A: 

Not sure if that the cause of your problem, but you seem to be missing the closing here :

 "'" . getMySQLDateString($buydate).

should be

 "'" . getMySQLDateString($buydate)."'"
Soufiane Hassou
@Soufiane Hassou : Sorry that was my mistake when copying this from sourcecode, it still has the same problem. So I would say that seems not to be the trouble-maker
Michael Mao
+1  A: 

In the following line:

$mysqlDateString = date('Y-m-d H:i:s', $strtotime($buydate)); 

Should this be:

$mysqlDateString = date('Y-m-d H:i:s', strtotime($buydate)); 

(eg. remove the dollar on the function) ?

David_001
@David_001 : Ahhh... This is the cause. I copied without thinking from the other thread...
Michael Mao
+1  A: 

This must be a comment but in sake of code formatting.

Despite of the fancy formatting, your code helps you nothing is such a case.
To make it much more useful, you have to add some debugging features in it.
Sensible variable names also helps
As well as removing function call from the string building

$sqldate = getMySQLDateString($buydate);
$sqldate = mysql_real_escape_string($sqldate);
//we name this variable $sql because it contains an SQL query
$sql = "INSERT INTO $tablename (barcode, book_name, volume_num,
        author, publisher, item_type, buy_price, buy_date) VALUES 
        (
        '$barcode',
        '$bookname',
        '$volumenum',
        '$author',
        '$publisher',
        '$itemtype',
        '$buyprice',
        '$sqldate'
        )";
//we name this variable $res because it's a resource 
//type variable contains query result
$res = mysql_query($sql) or trigger_error(mysql_error().htmlspecialchars($sql));

upon execution, this code will tell you comprehensive information on the error, if any occurred.

Col. Shrapnel
@Col. Shrapnel : totally agree, this makes the code must easier to code and read.
Michael Mao