0

我只是使用 SQL 查询从数据库中提取了不同的健身房,现在必须根据下拉结果将它们拉入。这是我到目前为止所拥有的。任何帮助将非常感激。

<?php 

require_once('connect.php'); //connecting to my database

mysql_select_db("gyms", $connect);

$result = mysql_query("SELECT * FROM gym WHERE id='1'"); //each gym has an "id" field of 1-5

while($row = mysql_fetch_array($result))
{
echo $row['name']; //all different attributes in the gym database
echo $row['type'];
echo $row['price'];
echo $row['hours'];
echo $row['parking'];
echo $row['facilities'];
}

?>
4

1 回答 1

0

下拉字段必须是表单的一部分。

if (!isset($_POST['gyms'])) {
echo '<form action="same.php" method="post">
    <select name="gyms">
        <option value="1">Gym 1</option>
        <option value="2">Gym 2</option>
    </select>
</form>';
} else {

    $id = $_POST['gyms'];
    // DO NOT forget to sanitize the value of $id before plugging it in
    // NEVER trust user input. Always assume it's a hacker.
    $result = mysql_query("SELECT * FROM gym WHERE id=$id");
    $row = mysql_fetch_array($result)
    // $row should contain the results from the database for the given gym
    var_dump($row);
}

顺便说一句,我强烈建议使用 PDO 而不是 mysql_*() 函数。以下是一些相关链接:

于 2012-12-03T22:13:00.080 回答