如何根据下拉菜单结果从数据库中提取信息

时间:2012-12-03 19:37:17

标签: php sql database drop-down-menu

我只是使用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'];
}

?>

1 个答案:

答案 0 :(得分: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 _ *()函数。以下是一些相关链接: