我正在尝试将当前日期插入数据库中数组中每个条目的日期。我已经找到了用于插入当前日期的文档,但是没有任何文件可以解释如何在数组中执行该操作。
<?php
$connect = new PDO("mysql:host=localhost;dbname=testing",
"root", "");
$query = "
INSERT INTO tbl_test
(full_name, id_number, email, pin_rank, team_name)
VALUES (:full_name, :id_number, :email, :pin_rank, :team_name)
";
for($count = 0; $count<count($_POST['hidden_full_name']); $count++)
{
$data = array(
':full_name' => $_POST['hidden_full_name'][$count],
':id_number' => $_POST['hidden_id_number'][$count],
':email' => $_POST['hidden_email'][$count],
':pin_rank' => $_POST['hidden_pin_rank'][$count],
':team_name' => $_POST['hidden_team_name']
);
$statement = $connect->prepare($query);
$statement->execute($data);
}
?>
我希望当前日期显示在表格的最后一列。任何帮助将不胜感激。
答案 0 :(得分:3)
假设您有某种日期列(# A tibble: 10 x 5
# Groups: Id [2]
Id Value Time av num_rows
<dbl> <dbl> <date> < dbl> <int>
1 121 2012-09-01 18 4
1 139 NA NA NA
1 157 NA NA NA
1 175 NA NA NA
1 193 NA NA NA
2 146 2012-09-01 21 4
2 167 NA NA NA
2 188 NA NA NA
2 209 NA NA NA
2 230 NA NA NA
,date
等),并且在我的示例datetime
中被命名,只需执行以下操作:
date_time
请注意,如果您除了$query = "
INSERT INTO tbl_test
(full_name, id_number, email, pin_rank, team_name, date_time)
VALUES (:full_name, :id_number, :email, :pin_rank, :team_name, NOW())
"
字段之外还拥有其他任何内容,那么您也将得到一些帮助。
答案 1 :(得分:1)
我不是数据库NOW
的忠实拥护者,因为数据库使用了自己的时区设置,而不是PHP。
因此,您可以在PHP中而不是在MySql中更改TimeZone,并以错误的日期结束(一次很难找到答案)。
正如已经显示的那样,我将向您展示如何仅使用PHP。
$query = "
INSERT INTO tbl_test
(full_name, id_number, email, pin_rank, team_name, created)
VALUES (:full_name, :id_number, :email, :pin_rank, :team_name, :created)
";
$today = (new DateTime)->format('Y-m-d H:i:s');
#$today = date('Y-m-d H:i:s'); //procedural
for($count = 0; $count<count($_POST['hidden_full_name']); $count++)
{
$data = array(
':full_name' => $_POST['hidden_full_name'][$count],
':id_number' => $_POST['hidden_id_number'][$count],
':email' => $_POST['hidden_email'][$count],
':pin_rank' => $_POST['hidden_pin_rank'][$count],
':team_name' => $_POST['hidden_team_name'],
':created' => $today
);
$statement = $connect->prepare($query);
$statement->execute($data);
}
您当然可以在循环中获取日期和时间,如果您需要第二精度,但是由于多次调用DateTime,效率较低。