Jesse
Jesse

Reputation: 23

Syntax error while trying to insert date with interval value to a date time field

I have the following code that is meant to insert values into the database and it looks like I am having problem around the DATE_ADD(NOW(), INTERVAL 2 DAY));

There is a field in the backend tabled called vkeyexpire which is a datetime column and I am passing a "s" as the parameter in the bind_param statement

I have tried and can't seem to get it working.

This is my code:

$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();

I am getting the below error message:

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

Upvotes: 0

Views: 409

Answers (2)

Jesse
Jesse

Reputation: 23

I was able to resolve the INSERT prepared statement in php Using DATE ADD using NOW(), + INTERVAL 2 DAY) in the value section of the insert statement.

In the bind_Param, do not include it in the string.

    $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

Upvotes: 0

Barmar
Barmar

Reputation: 781068

You can't put SQL functions in PHP code. You need to call DATE_ADD() in the SQL.

$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();

Upvotes: 1

Related Questions