PHP/PDO insert current date plus one month into MY

2019-02-16 03:23发布

问题:

I am trying to add the current date (not time if i have the option) and also the date of one month later into my MYSQL database and I keep gettin the following error:

Parse error: syntax error, unexpected T_LNUMBER in *myfile* on line 45

My function to insert the data is as follows:

function add_zipcode($zip, $adminID, $email)
{
    global $db;
    $query = '

        INSERT INTO zip_owners (zip, email, adminID, started, transferred, expires)
        VALUES (:zip, :email, :adminID, :started, :transferred, :expires)';
    try{
        $statement = $db->prepare($query);
        $statement->bindValue(':zip', $zip);
        $statement->bindValue(':email', $email);
        $statement->bindValue(':adminID', $adminID);
        $statement->bindValue(':started', now());

        $statement->bindValue(':transferred', now());
        $statement->bindValue(':expires', DATE_ADD(now(), INTERVAL 1 MONTH));
        $statement->execute();
        $statement->closeCursor();
    }
    catch (PDOexception $e) 
    {
        $error_message = $e->getMessage();
        echo "<p>Database Error: $error_message </p>";
        exit();
    }
}   

The problem line is this one:

$statement->bindValue(':expires', DATE_ADD(now(), INTERVAL 1 MONTH));

I'm not quite sure why that syntax doesn't work.

My goal is to be able to compare the dates when selecting rows to return the rows that have the expires filed within a week of the current date.

回答1:

Quote your MySQL statements, they're strings in PHP:

$statement->bindValue(':expires', 'DATE_ADD(now(), INTERVAL 1 MONTH)');