A
A
Alexey Sklyarov2018-04-10 11:51:53
PHP
Alexey Sklyarov, 2018-04-10 11:51:53

How to correctly add a date to a table entry?

The following error occurs when trying to create a record with data of type date:

Uncaught exception: 'PDOException'</p><p>Message: 'SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: ':end' for column 'timedate' at row 1'

The piece of code that is being executed:
$date = new \DateTime(date('Y-m-d'));
$date->add(new \DateInterval('P'.$period.'D'));
$endDate =$date->format('Y-m-d');

$status = 1;
$pdo = static::getDB();
$query = $pdo->prepare("INSERT INTO Invites (invite,timedate,status) VALUES (':code', ':end',':status')");
$query->bindParam(':code', $code, \PDO::PARAM_INT);
$query->bindParam(':end', $endDate, \PDO::PARAM_STR);
$query->bindParam(':status', $status, \PDO::PARAM_INT);
$query->execute();

The date is displayed in the required format (as in the database): 2018-05-10. What could be causing the problem?
PS I looked around stackoverflow with similar problems, I did not find a working solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sanovskiy, 2018-04-10
@0example

Remove the quotes from the query. They are like horseshoes on a cow.
VALUES (:code, :end,:status)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question