简体   繁体   中英

PHP/PDO insert current date plus one month into MYSQL

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM