简体   繁体   English

使用PHP和PDO搜索表单,然后在页面上显示结果

[英]Search form with PHP and PDO then display results on page

I am trying to search my SQL database with a simple search form and then return the data to the screen. 我正在尝试使用简单的搜索表单搜索我的SQL数据库,然后将数据返回到屏幕。

For example, the user can select a year, then all the row results from the table display all of the information for that entry. 例如,用户可以选择年份,然后表格中的所有行结果都将显示该条目的所有信息。 Here is my form: 这是我的表格:

<form class="" method="POST" action="availability.php"  enctype="multipart/form-data">

                    <select class="form-control mb-3"  name="year" id="year">
                      <option disabled selected>Year</option>
                      <option value="2019">2019</option>
                      <option value="2020">2020</option>
                      <option value="2021">2021</option>

                    </select>

                    <input class="btn btn-blue-grey" type="submit" value="submit">Search
                    <i class="fas fa-search ml-1"></i>
                    </input>

                    </form>

And here is my PHP 这是我的PHP

if(isset($_POST['submit'])) {

$year = $_POST['year'];

var_dump($year);
var_dump($_POST);

$sql = "SELECT * FROM availability WHERE year = :year";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':year',$year,PDO::PARAM_STR);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$data = $stmt->fetchAll(); }

and my HTML 和我的HTML

<ul>
                            <?php foreach($data as $stmt)  { ?>
                                <li><?php echo $stmt['cruise'];?></li>
                                <li><?php echo $stmt['year'];?></li>
                            <?php } ?>
                        </ul>

At the minute, the form submits, but I get nothing populating the list.. Any help would be appreciated. 此刻,表单提交了,但是我什么也没填。.任何帮助将不胜感激。

You can try to use this query : 您可以尝试使用以下查询:

$year = $_POST['year'];
$sql = "SELECT * FROM availability WHERE year = :year";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':year',$year,PDO::PARAM_STR);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$data = $stmt->fetchAll();

you must change a few thing, first, edit the select tag to: 您必须更改几件事,首先,将select标签编辑为:

<select class="form-control mb-3"  name="year" id="year">
                      <option disabled selected>Year</option>
                      <option value="2019">2019</option>
                      <option value="2020">2020</option>
                      <option value="2021">2021</option>

                    </select>

There is a condition that seems it is wrong. 有一个条件似乎是错误的。 so, change your html code to 因此,将您的html代码更改为

if($_POST) {

$year = $_POST['year'];

$stmt = $pdo->prepare("SELECT * FROM availability WHERE year = :year");
$stmt->execute(array("%$year%"));
// fetching rows into array
$data = $stmt->fetchAll();}

$stmt->bindParam(':year',$year,PDO::PARAM_STR); $ stmt-> bindParam(':year',$ year,PDO :: PARAM_STR); <-- does this need to be PARAM_INT ? <-是否需要为PARAM_INT?

also have you var_dump'ed $year = $_POST['year']; 也有你var_dump'ed $ year = $ _POST ['year']; to make sure you are getting a value? 确保您获得价值?

You could turn on error_reporting and see if you get any useful errors 您可以打开error_reporting并查看是否收到任何有用的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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