MySQL根据下拉值选择

时间:2015-03-16 23:26:01

标签: php html mysql

我有以下代码:

<?php
session_start();
include_once("config.php");

$query = "SELECT Category FROM books";
$result = mysqli_query ($mysqli, $query);

echo '<select name="dropdown" value=""><option value="">Dropdown</option>';
    while($row = mysqli_fetch_array($result))
    {
        echo '<option value="' . $row['Category'] . '">' . $row['Category'] . '</option>';
    }
    echo "</select>";

?>

从数据库中填充下拉框的值。 我想知道是否有一种方法可以在用户点击下拉菜单中的某个选项然后在表格中填充结果时运行一个select语句?

任何信息都会有所帮助!

由于

2 个答案:

答案 0 :(得分:0)

我假设$ mysqli是您的数据库连接,它是通​​过config.php创建的。我还假设该类别是books表中的列名。由您来清理和验证用户输入。这只是一个让你入门的例子。

page.php ....

<?php
session_start();
include_once("config.php");

function categories() {

    global $mysqli;
    $result = "";
    $stmt = "SELECT Category FROM books GROUP BY Category";
    $sql = mysqli_query ($mysqli, $stmt);
    while ($row = $sql->fetch_array(MYSQLI_BOTH))
    {
    $result .= '<option value="' . $row['Category'] . '">' . $row['Category'] . '</option>';
    }
    mysqli_free_result($sql);
    mysqli_close($mysqli);

    return $result;

}

IF (isset($_POST['ThisForm'])) {

  $category = htmlspecialchars(strip_tags(trim($_post['dropdown'])));
  $stmt = "SELECT * FROM books WHERE category ='$category'";
  $sql = mysqli_query ($mysqli, $stmt);
  while ($row = $sql->fetch_array(MYSQLI_BOTH))
  {
    // do something with result

  }
  // free result and close connection
  mysqli_free_result($sql);
  mysqli_close($mysqli);

}ELSE{
  // base form 
  echo '<form action="page.php" name="something" method="post">';
  echo '<select name="dropdown" value=""><option value="">Dropdown</option>'.categories().'</select>';
  echo '<input type="submit" name="ThisForm" value="submit" />';
  echo '<form>';
}
?>

答案 1 :(得分:0)

好的,共振81,你想要根据所选的选项填充一个表,下一个代码完全符合你的要求,解释就在之后:

<html>
  <head>
    <title>My list</title>
    <script type="text/javascript">
//----------------------------------------------------------------
// SENDS SELECTED OPTION TO RETRIEVE DATA TO FILL TABLE.
function send_option () {
var sel = document.getElementById( "my_select" );
var txt = document.getElementById( "my_option" );
txt.value = sel.options[ sel.selectedIndex ].value;
var frm = document.getElementById( "my_form" );
frm.submit();
}
//----------------------------------------------------------------
    </script>
  </head>
  <body>

    Click on any option
    <br/>
    <select id="my_select" onchange="send_option();">
      <option>Select an option</option>
<?php
//----------------------------------------------------------------
// LIST FILLED FROM DATABASE (ALLEGEDLY).
for ( $i = 0; $i < 5; $i++ )
{ $text = chr($i+65) . chr($i+65) . chr($i+65);
  echo "<option value='" . $text . "'>" . $text . "</option>";
}
//----------------------------------------------------------------
?>
    </select>
    <br/> 
    <br/>
    <table>
<?php
//----------------------------------------------------------------
// TABLE FILLED FROM DATABASE ACCORDING TO SELECTED OPTION.
if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION.
     for ( $i = 0; $i < 4; $i++ ) // DISPLAY ROWS.
     { echo "<tr>";
       for ( $j = 0; $j < 6; $j++ ) // DISPLAY COLUMNS.
         echo "<td>" . $_POST["my_option"] . "</td>"; // DISPLAY OPTION.
       echo "</tr>";
     }
else echo "<tr><td>Table empty</td></tr>";
//----------------------------------------------------------------
?>
    </table>

<!-- FORM TO SEND THE SELECTED OPTION. -->
    <form method="post" action"01.php" style="display:none" id="my_form">
      <input type="text" id="my_option" name="my_option"/>
    </form>

  </body>
</html>

为了让您(以及对我而言)更容易,我不使用数据库,您只需将以前的代码复制粘贴到文本文件中,重命名为&#34; 01.php&#34; (因为那是表单的动作,你可以改变它),并在你的浏览器中运行它,随时可以使用。

下拉列表从数据库填充(在本例中为字母),当选择一个选项时,页面将使用所选选项重新加载并填充表格。

你说:&#34;当用户点击下拉菜单中的一个选项然后在表格中填充结果时,将运行的选择语句&#34;。你想要的这个选择语句必须把它放在行之后:

if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION.

因此,您的select语句将从$ _POST中选择所选项并使用它来检索正确的数据并显示它。

让我知道它是否对你有所帮助。

这是填写下拉列表的代码,我的代码与您合并:

// LIST FILLED FROM DATABASE (ALLEGEDLY).
$query = "SELECT Category FROM books";
$result = mysqli_query ($mysqli, $query);
while ( $row = mysqli_fetch_array($result) )
  echo "<option value='" . $row['Category'] . "'>" . $row['Category'] . "</option>";

接下来的编辑就是填表。如果查询不正确,请更改正确的查询:

// TABLE FILLED FROM DATABASE ACCORDING TO SELECTED OPTION.
$query = "SELECT Category FROM books where category like '" . $_POST["my_option"] . "'";
$result = mysqli_query ($mysqli, $query);
while( $row = mysqli_fetch_array($result) )
  echo "<tr>" .
       "<td>" . $row['book_name'] . "</td>" .
       "<td>" . $row['author'] . "</td>" .
       "<td>" . $row['Category'] . "</td>" .
       "</tr>";