views:

162

answers:

2

I have the following code:

function dbInsert()
{
    global $dbcon, $dbtable;
    global $inputData;

    $sqlQuery = 'INSERT INTO ' . $dbtable . ' (id_author, date, title, description) VALUES (?, ?, ?, ?)';
    $stmt = $dbcon->prepare($sqlQuery);
    echo $sqlQuery;
    $stmt->bind_param('isss', $inputData['idAuthor'], $inputData['date'], $inputData['title'], $inputData['description']);
    $stmt->execute();
    $stmt->close();
    header('Location: ./news.php?order=' . $_GET['order'] . '&sort=' .  $_GET['sort'] . '&filter=' .  $_GET['filter'] . '&page=' .  $_GET['page'] . '');
}

$inputData['date'] is formatted as dd.mm.yyyy ex (18.02.2010)

I couldnt find a date parameter so I am trying to insert it as a string, is this right? I assume I need to tell mysql how to handle the input but could not find anything on the subject. The equivalent in ORACLE would be TO_DATE(?, 'dd.mm.yyyy') if it helps clarify what i am asking.

+1  A: 

You could take a look at the STR_TO_DATE function. It takes a format parameter. The allowed values for the format parameter are described here.

$sqlQuery = 'INSERT INTO ' . $dbtable . ' (id_author, date, title, description)' .
            ' VALUES (?, STR_TO_DATE(?, \'%d.%m.%Y\'), ?, ?)';
Mark Byers
+1  A: 

You can use a date/time column

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-types.html

and use the STR_TO_DATE function:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date

Or you can convert it to a unix timestamp and insert this one in an INT Column.

PvB