如何使用下拉菜单中的选定选项查询我的数据库?

时间:2018-04-08 14:21:30

标签: php mysql pdo

我正在使用PDO与我的数据库进行交互。如何使用用户选择的输入来查询我的数据库。这就是我到目前为止所拥有的。我将所选选项存储为变量,但实际上并未捕获。我该怎么做才能解决这个问题?

  echo <<<FORMSTART
  <form name= "name" method= "post">
   <select name = "name" onChange="document.topic_list.submit()">
   <option value = "None">Select name</option>
FORMSTART;

    $stmt = $pdo->query("SELECT DISTINCT name from users");

    //populate drop down menu
    while ($row = $stmt->fetch()) {

        echo "<option value= '" . $row['name'] . "'>" . $row['name'] . "</option>";
    }

    //End form for drop down menu
    echo <<<FORMEND
</select>

</form>
FORMEND;

    $selected_name = $_POST["name"];
    //This prints here but when I try to use this variable later it doesnt print anything
    echo "$selected_name Selected";

0 个答案:

没有答案