有没有一种方法可以将变量放入prepare()方法中,以便从mysqlite数据库中获取数据?

问题描述 投票:0回答:1

程序的目的是让用户从表单中选择一个国家,并显示您选择的国家中的所有博物馆,并在另一页中显示。

<?php
$museumDb = new PDO('sqlite:museums.db');
$query = $museumDb->prepare('SELECT * FROM museums');
$query->execute();
print '<pre><code>';
$museums = $query->fetchAll(PDO::FETCH_ASSOC);
print '</code></pre>';
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="style.css" type="text/css" rel="stylesheet">
    <title>Museums</title>
</head>

<body>
    <form action="result.php" method="POST">
        <label for="country">Pick A Country:</label>
        <select id="country" name="country">
            <option value="" disabled selected="selected">---Pick a state---</option>
            <option value="France">France</option>
            <option value="Russia">Russia</option>
            <option value="China">China</option>
            <option value="United States">United States</option>
            <option value="Vatican City">Vatican City</option>    
            <option value="Japan">Japan</option>
            <option value="Mexico">Mexico</option>
            <option value="United Kingdom">United Kingdom</option>
            <option value="South Korea">South Korea</option>
            <option value="Belgium">Belgium</option>
            <option value="Israel">Israel</option>
            <option value="Singapore">Singapore</option>
            <option value="Italy">Italy</option>
            <option value="Spain">Spain</option>
            <option value="Egypt">Egypt</option>
            <option value="Brazil">Brazil</option>
            <option value="Austria">Austria</option>
            <option value="Czech Republic">Czech Republic</option>
            <option value="Germany">Germany</option>
            <option value="Australia">Australia</option>
            <option value="India">India</option>
            <option value="Netherlands">Netherlands</option>
            <option value="Canada">Canada</option>
            <option value="Hungary">Hungary</option>
            <option value="Switzerland">Switzerland</option>
            <option value="Poland">Poland</option>
            <option value="Taiwan">Taiwan</option>
            <option value="Tunisia">Tunisia</option>
            <option value="Norway">Norway</option>
            <option value="UAE">UAE</option>
            <option value="Greece">Greece</option>
        </select>
        <br>
        <input type="submit" value="Submit">
    </form>
    <table>
        <tr>
            <th>ID#</th>
            <th>Museum Name</th>
            <th>Country</th>
        </tr>
        <?php foreach ($museums as $museum) : ?>
            <tr>
                <td><?php print $museum['museum_id']; ?></td>
                <td><?php print $museum['museum_name']; ?></td>
                <td><?php print $museum['country']; ?></td>
            </tr>
        <?php endforeach; ?>
    </table>
</body>
</html>

这是应该根据所选择的国家/地区得出数据的页面,我真的不知道为什么不起作用,我是php的新手,所以我可能忽略了某些内容,如果能得到帮助,我将不胜感激。

<?php
require_once('index.php');
$countries = $_POST['country'];
$filterQuery ="SELECT museum_id,museum_name FROM museums WHERE country = ?";
$filter = $museumDb -> prepare($filterQuery);
$filter->bindParam('?',$countries);
$filter->execute();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Query results</title>
</head>
<body>
<table>
        <tr>
            <th>ID#</th>
            <th>Museum Name</th>
            <th>Country</th>
        </tr>
        <?php while($museumSearch = $filter->fetch(PDO::FETCH_OBJ)) {
            print '<tr>';
            print'<td>'.$museumSearch->museum_id.'</td>';
            print'<td>'.$museumSearch->museum_name.'</td>';
            print'<td>'.$museumSearch->country.'</td>';
            print'</tr>';
        }
        ?>
    </table>
</body>
</html>
php sqlite pdo
1个回答
0
投票

[当使用?作为占位符时,请在bindParam()中使用数字索引。

$filter->bindParam(0, $countries);

[在使用命名占位符时使用字符串索引。

$filterQuery ="SELECT museum_id,museum_name FROM museums WHERE country = :country";
$filter = $museumDb -> prepare($filterQuery);
$filter->bindParam(':country',$countries);
© www.soinside.com 2019 - 2024. All rights reserved.