0

我有一个基本的 SQL 查询,它根据输入到搜索字段中的内容获取一些数据 - 我已经放置了一个从数据库中的列填充的选择框 - 我想做的是当它被改变时/post,它会更新已经找到的结果,即通过游戏的“类型”进一步深入研究它们。

这是我已经拥有的代码

  echo "<select id='dropdown' name='dropdown' class='dropdown'>";//creates select HTML element
    $stmt = $dbh->prepare('SELECT genre_name FROM genre'); //prepares sql query
    $stmt->execute(); //executes query
       while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //fetches data in associative array
    echo "<option>{$row['genre_name']}</option>"; //inputs data found into select as an option
       }       
echo "</select>";




echo '<input type="submit" value="Filter" id="filter" name="filter" class="filter">';

if(!isset($_POST['filter']))//check if filter genre has been pressed
{
    echo ("<h4>Please select a genre</h4>"); //if not show this
}
    else { //otherwise if it has...do this
    echo ("<h4>Your results have been filtered</h4>");

    $dropvalue = $_POST['dropdown']; //sets variable of value of dropdown box
    $dbh = config();
            //sorting function, want to further drill search results by genre
            $stmt = $dbh->prepare("SELECT * FROM consoles, games, genre WHERE genre.genre_name = :dropvalue");
            $stmt->bindValue(':dropvalue','%'.$dropvalue.'%');
            $stmt->execute();           
            while ($row = $stmt->fetch())
{
    echo "<ul>";
    echo "<a href='details.php?game_id=".$row['game_id']."'>".$row['game_name']."</a>";
    echo "</ul>";
}
}
      $dbh = NULL; //terminates connection to database
?>
4

2 回答 2

0

您可以使用 Javascript 来做到这一点。做就是了 :

1)在标签<form action="" method="post" id="drop_submit">前添加<select>

2)写<select id="select_tag" name="YOUR_SELECT_NAME">“PHP DB代码选项”</select>

3) 添加以下 javascript 以在 HTML 页面的任何位置提交您的表单。

<script type="text/javascript">
  $('#status').change(function ()
  {
    $('#drop_submit').submit();
  });
</script>

是的,别忘了补充,

if(isset($_POST['dropdown']))
{
  $dropdown = $_POST['dropdown'];
}
else
{
  $dropdown = "YOUR_DEFAULT / 1ST VALUE";
}
  $stmt = $dbh->prepare("SELECT game_name FROM games WHERE games.game_id=('$dropdown')"); 
  //prepares sql statement, this prevents SQL Injection, as the query is sent seperate to the string entered.
  $stmt->bindValue(':dropdown','%'.$dropdown.'%');
  $stmt->execute();
于 2013-01-24T20:35:01.910 回答
0

当您更新您的问题时,这是解决方案

echo '<form action="" method="post">';

     echo "<select id='dropdown' name='dropdown' class='dropdown'>";//creates select HTML element
        $stmt = $dbh->prepare('SELECT genre_name FROM genre'); //prepares sql query
        $stmt->execute(); //executes query
           while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //fetches data in associative array
        echo "<option>{$row['genre_name']}</option>"; //inputs data found into select as an option
           }       
    echo "</select>";        
    echo '<input type="submit" value="Filter" id="filter" name="filter" class="filter">';

    echo '</form>';

    if(!isset($_POST['filter']))//check if filter genre has been pressed
    {
        echo ("<h4>Please select a genre</h4>"); //if not show this
    }
        else { //otherwise if it has...do this
        echo ("<h4>Your results have been filtered</h4>");

        $dropvalue = $_POST['dropdown']; //sets variable of value of dropdown box
        $dbh = config();
                //sorting function, want to further drill search results by genre
                $stmt = $dbh->prepare("SELECT * FROM consoles, games, genre WHERE genre.genre_name = :dropvalue");
                $stmt->bindValue(':dropvalue','%'.$dropvalue.'%');
                $stmt->execute();           
                while ($row = $stmt->fetch())
    {
        echo "<ul>";
        echo "<a href='details.php?game_id=".$row['game_id']."'>".$row['game_name']."</a>";
        echo "</ul>";
    }
    }
          $dbh = NULL; //terminates connection to database
    ?>
于 2013-01-25T20:50:17.773 回答