我有一个带有日期和时间的JS datepicker的PHP,它将这个格式的所有字符串"19.08.2012 @ 20:18"保存在一个名为$ date的变量中.
如果我将此变量直接放在查询中,它会在数据库中写入"0000-00-00 00:00:00".目前我头上有一个大黑洞,所以需要帮助.
如何在数据库中写入正确的选定日期和时间?
对不起,我认为不是那么重要,这是代码.
的index.php
<script type="text/javascript">
$(function() {
$("#date").datetimepicker({
dateFormat:'dd.mm.yy',
separator: ' @ '
});
});
</script>
<form action="setting.php" method="post">
<table>
<tr>
<td><label for="date">Date:</label></td>
<td><input type="text" id="date" name="date"/></td>
</tr>
</table>
<input type="submit" name="submit" />
Run Code Online (Sandbox Code Playgroud)
setting.php
if(isset($_POST['save_autoresp'])) {
create($_POST['date']);
header("Location: admin.php");
exit();
} else {
header("Location: index.php");
exit();
}
Run Code Online (Sandbox Code Playgroud)
的functions.php
.......
.......
// Write the fields to the table
$query_create = mysqli_query($link, "INSERT INTO table VALUES (null, '$id', '$date')") or die ('Could not connect: ' . mysqli_error($link));
.......
.......
Run Code Online (Sandbox Code Playgroud)
MySQL数据库中的日期字段是"datetime".
您收到的日期采用MySQL无法识别的格式.您必须先将其转换为可接受的日期和时间文字,然后您可以将其传递给INSERT.
这种转换可以通过两种方式完成:
无论你会选择,固守年 - 月 - 日格式的日期保存到数据库时,这样你就不会得到找出疯狂为什么"08/02"被认为是八月二日,而不是2月8日.
| 归档时间: |
|
| 查看次数: |
2184 次 |
| 最近记录: |