如何在mysql中插入日期作为参数? [英] How do I insert a date into mysql as a parameter?
问题描述
我有以下代码:
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']格式为dd.mm.yyyy ex (18.02.2010)
我找不到一个日期参数,所以我试图将它作为一个字符串插入,是吗?我假设我需要告诉mysql如何处理输入,但找不到任何东西。 ORACLE中的等价物将是TO_DATE(?,'dd.mm.yyyy'),如果它有助于澄清我所要求的。
p>您可以使用日期/时间列
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-types.html
并使用STR_TO_DATE函数:
http://dev.mysql.com/doc/refman/5.1/en/date-and- time-functions.html#function_str-to-date
或者您可以将其转换为unix时间戳,并将其插入到INT列中。 p>
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.
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.
这篇关于如何在mysql中插入日期作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!