php - 如何将输入时间的值插入到数据库表中?

标签 php mysql

我目前正在开发一个考勤系统,用户应该使用 <input type="time"> 指定他的上类时间和超时时间。 ,但是当我尝试提交表单时,它不断向我发送错误

这是一段 HTML 代码

<tr>
    <td><input type="text" id="day1" name="day1" class="date-field" readonly></td>
    <td><input type="time" id="day1-in" name="day1-in" class="time-field" title="Ex: 08:00 AM"></td>
    <td><input type="time" id="day1-out" name="day1-out" class="time-field" title="Ex: 05:00 PM"></td>
    <td><input type="checkbox" name="ot-1-a" id="ot-1-a" value="1"></td>
    <td><input type="checkbox" name="ot-1-b" id="ot-1-b" value="1"></td>
    <td><input type="checkbox" name="ot-1-c" id="ot-1-c" value="1"></td>
    <td><input type="text" id="day1-hrs-served" class="dtr-field" name="day1-hrs-served" readonly></td>
    <td><input type="text" id="day1-reg-ot" class="dtr-field" name="day1-reg-ot" readonly></td>
    <td><input type="text" id="day1-reg-hol-ot" class="dtr-field" name="day1-reg-hol-ot" readonly></td>
    <td><input type="text" id="day1-spe-hol-ot" class="dtr-field" name="day1-spe-hol-ot" readonly></td>
    <td><input type="text" id="day1-reg-hol-ot-ot" class="dtr-field" name="day1-reg-hol-ot-ot" hidden readonly></td>
    <td><input type="text" id="day1-spe-hol-ot-ot" class="dtr-field" name="day1-spe-hol-ot-ot" hidden readonly></td>
</tr>

我的 PHP 代码片段

$day1 = mysqli_real_escape_string($conn, $_REQUEST['day1']);
$day1In = mysqli_real_escape_string($conn, $_REQUEST['day1-in']); 
$day1Out = mysqli_real_escape_string($conn, $_REQUEST['day1-out']);
$day1HrsServed = mysqli_real_escape_string($conn, $_REQUEST['day1-hrs-served']);
$day1RegOT = mysqli_real_escape_string($conn, $_REQUEST['day1-reg-ot']);
$day1RegHol = mysqli_real_escape_string($conn, $_REQUEST['day1-reg-hol-ot']);
$day1SpeHol = mysqli_real_escape_string($conn, $_REQUEST['day1-spe-hol-ot']);
$day1RHOT = mysqli_real_escape_string($conn, $_REQUEST['day1-reg-hol-ot-ot']);
$day1SHOT = mysqli_real_escape_string($conn, $_REQUEST['day1-spe-hol-ot-ot']);

$daily = "INSERT INTO daily_records (ref_dtr, record_date, record_in, record_out, hrs_served, reg_ot, reg_hol, spe_hol, rh_ot, sh_ot) VALUES ($dtr_ref, $day1, $day1In, $day1Out, $day1HrsServed, $day1RegOT, $day1RegHol, $day1SpeHol, $day1RHOT, $day1SHOT)";

if(mysqli_query($conn, $daily)){
   echo "Records added successfully.";
} 
else{
   echo "ERROR: Could not able to execute $daily. " . mysqli_error($conn);
}

当我提交此表单时,它会抛出错误

ERROR: Could not able to execute INSERT INTO daily_records (ref_dtr, record_date, record_in, record_out, hrs_served, reg_ot, reg_hol, spe_hol, rh_ot, sh_ot) VALUES (5d7f686865d1a, 2019-09-01, 08:00 AM, 05:00 PM, 8.00, 0.00, 0, 0, 0, 0). You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':00 AM, 05:00 PM, 8.00, 0.00, 0, 0, 0, 0)' at line 4

我怀疑是输入时间的问题,目前是record_inrecord_out数据类型是时间,我也尝试将其更改为 varchar 但它仍然抛出错误。有人可以指出我的错误或任何改进我的代码的建议吗,谢谢。

更新 因为我使用的输入时间是 12 小时格式,所以我使用了

$day1InCon = date('H:i', strtotime($day1In));
$day1OutCon = date('H:i', strtotime($day1Out));

将其转换为 24 小时格式。在数据库中插入 12 小时格式似乎有问题,但我不确定。

最佳答案

在 SQL 值中,必须使用 quote(') 作为字符串参数。

示例:

$query = "INSERT INTO table (name, surname, address) VALUES ( '" . $name . "', '" . $surname . "', '" . $address . "' )";

关于php - 如何将输入时间的值插入到数据库表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57954764/

相关文章:

mysql - fatal error : Call to a member function result() on a non-object in code igniter model working fine in localhost

php - 如何生成私有(private)函数 SELECT * FROM 批量结果?

php - curl 跟随 POST 位置

php - Yii INSERT ... 重复更新

php - 尝试插入 mysql 数据库忽略空白字段

mysql - 我可以从 SQL DB 模式在 Symfony2 中构建实体吗?

mysql - 无法将值数组存储到 Rails 2.x 中的单个数据库表列?

mysql - Oracle 需要类似于 INFORMATION_SCHEMA 为 MySQL 提供的查询

php - Laravel Eloquent 按关系排序

php - 地址对象中带有 PHP 的 Google API JSON 邮政编码