1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[ エラー ] PHP MySQL SQL文 日付で範囲指定し絞り込む

Last updated at Posted at 2018-07-09

#Datepickerを使用し、日付で絞り込みをする

SQL文で、絞り込みをしましたがエラーでつまづいたので :frowning2:

index.html
<div id="datepicker">
 <input type="text" name="date_start">
 <input type="text" name="date_end">
</div>
date.php
$start = $_POST['date_start'];
$end = $_POST['date_end'];

$sql = "SELECT * FROM news WHERE created_at BETWEEN $start AND $end";

上記の書き方だと、絞り込みができませんでした。

  • 絞り込み条件の部分には、プレースホルダを使用する
date.php
$start = $_POST['date_start'];
$end = $_POST['date_end'];

$sql = "SELECT * FROM news WHERE created_at BETWEEN start = ? AND end = ?";

$stmt->bindValue(1, $start, PDO::PARAM_STR);
$stmt->bindValue(2, $end, PDO::PARAM_STR);

$stmt = $pdo->prepare($sql);
$stmt->execute(array($start,$end));
1
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?