php 使用准备好的语句插入日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11396604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert a datetime with prepared statement
提问by bryanblackbee
I am trying to use prepared statements to insert a datetime for a library application. Here is the code thus far:
我正在尝试使用准备好的语句为库应用程序插入日期时间。这是迄今为止的代码:
global $dbh;
$query = "INSERT INTO `loan` SET
`title` = (?), //example value - Lord of the Rings
`description` = (?), //example value - Trilogy
`start_date` = (?), //example value 20120701 in String datatype
`end_date` = (?)"; //example value 20120702 in String datatype
$statement = $dbh->prepare($query);
$statement->bind_param("ssss", $title,$description,$startDate,$endDate);
$statement->execute();
print $statement->error; //to check errors
$statement->close();
However, I cannot seem to insert this value into the row. At the same time, somehow the
但是,我似乎无法将此值插入到行中。与此同时,不知何故
print $statement->error
does not seem to display any error.
似乎没有显示任何错误。
Any help will really do.
任何帮助都可以。
UPDATE:
更新:
It actually works. I was just referencing the wrong database. But I want to add a little outro for new people who chanced upon this.
它确实有效。我只是引用了错误的数据库。但我想为偶然发现这一点的新人添加一些结尾。
Remove all the comments as mentioned in the comments/answers as they will mess up your string.
For DATETIME, remember to specify the datatype as String as MySQL only recognises String datatype. If you are not using prepared queries, this means you have to add '' quotes for the values.
The insertion will result in the date (2012-07-01 00:00:00) format as time is not specified.
Both SQL queries work. INSERT INTO tbl_name SET col_name = value or INSERT INTO tbl_name(col_name) VALUES (value) work.
删除评论/答案中提到的所有评论,因为它们会弄乱您的字符串。
对于 DATETIME,请记住将数据类型指定为 String,因为 MySQL 仅识别 String 数据类型。如果您没有使用准备好的查询,这意味着您必须为值添加 '' 引号。
由于未指定时间,因此插入将导致日期 (2012-07-01 00:00:00) 格式。
两个 SQL 查询都有效。INSERT INTO tbl_name SET col_name = value 或 INSERT INTO tbl_name(col_name) VALUES (value) 工作。
回答by Valeh Hajiyev
Try something like this:
尝试这样的事情:
global $dbh;
$query = "INSERT INTO loan (title, description, start_date, end_date) VALUES (?,?,?,?)"
$statement = $dbh->prepare($query);
$statement->bind_param("ssss", $title,$description,$startDate,$endDate);
$statement->execute();
print $statement->error; //to check errors
$statement->close();
回答by DrinkJavaCodeJava
Assuming your form input for your date is a type input named "date", here is what you do. In php type
假设您的日期表单输入是一个名为“日期”的类型输入,这就是您要做的。在 php 类型中
$date = $_POST['date']
$query = "INSERT INTO `loan` SET
`title` = (?),
`description` = (?),
`start_date` = ".$date. //Insert variable here
"`end_date` = (?)";
I know it's not good practice to insert a date in one single type input but I am just using this example for simplicity only. The proper way you can figure out yourself.
我知道在一个单一类型的输入中插入日期不是一个好习惯,但我只是为了简单起见使用这个例子。正确的方法你可以自己弄清楚。

