【问题标题】:Use Selected Option for SQL-Query (PHP)使用 SQL 查询的选定选项 (PHP)
【发布时间】:2018-04-25 08:00:40
【问题描述】:

我有一个包含三列的数据库:idKernfragen;核心;吉比特

我想使用“Gebiet”中的选项设置一个下拉列表 - 这工作正常。我还设法从数据库中选择了所有信息。但是,我想使用下拉菜单中的选定选项作为变量,并将“WHERE”添加到我的 SQL 查询中,以便仅显示 where 'Gebiet'=Dropdown-Selection。

这是我的代码:

<?php
require_once ('config.php');
$db = mysqli_connect (
                     MYSQL_HOST, 
                     MYSQL_BENUTZER, 
                     MYSQL_KENNWORT, 
                     MYSQL_DATENBANK
                    );
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <div id="select">
    <select name="gebiet1" onchange="this.form.submit();">

<?php
$gebietausgabe = mysqli_query($db, "SELECT DISTINCT Gebiet FROM kernfragen");
while ($row = $gebietausgabe->fetch_assoc())
{
  echo "<option value=\"gebiet1\">" . $row['Gebiet'] . "</option>";
}

?>

    </select>
    </div>
</form>

<br>
<br>

<?php        
$ergebnis = mysqli_query($db, "SELECT idKernfragen, Kernfrage FROM kernfragen");

while($row = mysqli_fetch_object($ergebnis))
{
  echo $row->idKernfragen;
  echo '.) ';
  echo $row->Kernfrage;
  print "<br>";
}
?>

非常感谢!

【问题讨论】:

  • 在查询中使用where gebiet = $_POST['gebiet1']

标签: php sql select where dropdown


【解决方案1】:

试试这个:

<?php
if(isset($_POST['gebiet1'])){
    $selected_val = $_POST['gebiet1'];  // Storing Selected Value In Variable
    echo "You have selected :" .$selected_val;  // Displaying Selected Value
}
?>

<?php
require_once ('config.php');
$db = mysqli_connect (
                     MYSQL_HOST, 
                     MYSQL_BENUTZER, 
                     MYSQL_KENNWORT, 
                     MYSQL_DATENBANK
                    );
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <div id="select">
    <select name="gebiet1" onchange="this.form.submit();">

<?php
$gebietausgabe = mysqli_query($db, "SELECT DISTINCT Gebiet FROM kernfragen");
while ($row = $gebietausgabe->fetch_assoc())
{

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

?>

    </select>
    </div>
</form>

<br>
<br>

<?php        
$ergebnis = mysqli_query($db, "SELECT idKernfragen, Kernfrage FROM kernfragen where Gebiet='".$selected_val."'");

while($row = mysqli_fetch_object($ergebnis))
{
  echo $row->idKernfragen;
  echo '.) ';
  echo $row->Kernfrage;
  print "<br>";
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 2011-06-28
    • 1970-01-01
    相关资源
    最近更新 更多