尝试将带间隔值的日期插入日期时间字段时出现语法错误

时间:2019-10-10 02:38:46

标签: php mysql

我有以下代码打算将值插入数据库中,并且似乎在“ DATE_ADD(NOW(),INTERVAL 2 DAY));周围遇到问题;

后端表中有一个名为vkeyexpire的字段,该字段是日期时间列,我在bind_param语句中传递了“ s”作为参数

我已经尝试过并且似乎无法正常工作

这是我的代码:

$sql = "INSERT INTO register (firstname, lastname, email,password, classof,country, town, comment,verified, vkey,vkeyexpire) VALUES(?, ?, ?, ?, ?,?,?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param('ssssssssiss', $firstname, $lastname,$email,$password,$classof,$country,$town,$comment, $verified, $vkey, DATE_ADD(NOW(), INTERVAL 2 DAY));
$stmt->execute();

我收到以下错误消息:

Parse error: syntax error, unexpected '2' (T_LNUMBER), expecting ')' in C:\xampp\htdocs\USS\controllers\register_server.php on line 110

2 个答案:

答案 0 :(得分:0)

您不能将SQL函数放在PHP代码中。您需要在SQL中调用DATE_ADD()

$sql = "INSERT INTO register (firstname, lastname, email,password, classof,country, town, comment,verified, vkey,vkeyexpire) 
        VALUES(?, ?, ?, ?, ?,?,?,?,?,?,DATE_ADD(NOW(), INTERVAL 2 DAY))";
$stmt = $conn->prepare($sql);
$stmt->bind_param('ssssssssis', $firstname, $lastname,$email,$password,$classof,$country,$town,$comment, $verified, $vkey);
$stmt->execute();

答案 1 :(得分:0)

我能够在insert语句的值部分中使用NOW()+ INTERVAL 2 DAY)在DATE ADD中解析php中的INSERT准备语句。

在bind_Param中,请勿将其包括在字符串中。

    $sql = "INSERT INTO register (firstname, lastname, email,password, classof,country, town, comment,verified, vkey,vkeyexpire) VALUES(?, ?, ?, ?, ?,?,?,?,?,?,now() + INTERVAL 2 day)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('ssssssssis', $firstname, $lastname,$email,$password,$classof,$country,$town,$comment, $verified, $vkey);
        if ($stmt->execute())
        {

enter image description here