I've got a php script that adds a row into a db table with a DATE formatted column, e.g. 2016-11-02.
My query adds data to the table like so:
$duser = $_POST['username'];
$tdate = date("Y-m-d");
//build query
$query = "
INSERT INTO emodata (
username,
date
) VALUES (
':duser',
':tdate'
)
";
// create tokens
$query_params = array(
':duser' => $duser,
':tdate' => $tdate
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query - $tdate : " . $ex->getMessage());
}
For some reason mySQL doesn't seem to think the format is correct. The error I receive is:
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: ':tdate' for column 'date' at row 1
I've checked the php date format and it does produce YYYY-MM-DD format, which is the correct format for mysql DATE...
Do you guys think this might be to do with the prepared statement and parameter substitution? If so, is there a way round this or do I have to use some other form of secure query?