Question: Question:
From the html form submission, I try to compare the value of type = "date" with the date type column in SQL, but I can't extract it.
The source code is as follows.
Is there something wrong with the SQL, PHP, html form description?
<form action="result.php" method="post">
<input type="date" name="search_date">
<input type="submit">
</form>
//event_dateカラムはdate型です
$search_date = $_POST["search_date"];
$sql = "select * from sample where event_date = ?";
$stmt = $dbh->prepare($sql);
//DBハンドラー用意済み
$stmt->execute($stmt, $search_date);
By the way, can the value of type = "date" be used as the extraction condition for the date type column?
Answer: Answer:
The values submitted by the form are in ISO 8601 format
2015-08-01T02:03:04+09:00
The date and time are separated by T
, and the time zone specification is added at the end.
On the other hand, the format that MySQL can recognize is
- 15-08-01
- 2015-08-01
- 2015-08-01 02:03:04
There are 6 types of convenience of the 3 types of symbol differences and the non-symbol version. So the SQL in question is not recognized as a date, it seems that event_date
is converted to a string other than ISO 8601 and then compared to the parameter.
In order to recognize it as a date, PHP should convert it like date('Ym-d', strtotime($_POST["search_date"]))
.