使用下拉列表从mysql数据库中获取数据,然后使用搜索功能从选定的下拉列表选项中过滤数据

时间:2014-05-29 09:55:33

标签: javascript php html mysql sql

我想通过搜索功能(搜索栏和" Ok"按钮)来使用下拉列表(带过滤器/选项)。

当我在下拉列表中选择过滤器/选项时,例如" Team"。下拉 然后列表应该知道该列表已更改为过滤器/选项" Team"。 在此之后,我使用搜索功能输入搜索词,如" USA"然后按 好的按钮。 按下确定按钮后,会出现一个sql-query:SELECT * FROM mytable WHERE Team =' USA&#39 ;; 生成以获取db" mytable"。

中的所有匹配行
  1. 选择过滤器" Team"。
  2. 选择一个团队," USA"。
  3. 使用db mytable中的相关数据获取匹配的行。
  4. 希望我让自己更好地理解,谢谢大家的时间!

    这是我的代码:

    <html>
        <head> 
            <title>AiFind</title>
            <link rel="stylesheet" href="Style.css">
            <script src="logic.js"></script>
    
        </head>
        <body>
            <h1>AiFind</h1>
        </body>
    
    </html>
    
    
    <?php
    
    include "connection.php";
    
    $sql = "SELECT * FROM mytable";
    
    if (isset($_POST['search'])) {
    
        $search_term = mysql_real_escape_string($_POST['search_box']);
    
        $sql .= " WHERE F_ar = '$search_term' ";
        $sql .= " OR Postnr = '$search_term' ";
        $sql .= " OR Postort = '$search_term' ";
        $sql .= " OR Vardgivare = '$search_term' ";
        $sql .= " OR Team = '$search_term' ";
        $sql .= " OR Orsak = '$search_term' ";
        $sql .= " OR Planerat_datum = '$search_term' ";
        $sql .= " OR fran = '$search_term' ";
        $sql .= " OR AAA_diam = '$search_term'; ";
    }
    
    $query = mysql_query($sql) or die(mysql_error());
    
    ?>
    
    <form name="Select_filter" method="POST" action="VGR_data_display.php">
    <select id="dropdown" name="filter">
    <option value=""></option>
    <option value="1">ID</option>
    <option value="2">Alder</option>
    <option value="3">Postnummer</option>
    <option value="5">Postort</option>
    <option value="6">Vårdgivare</option>
    <option value="7">Planerat Datum</option>
    <option value="8">Status</option>
    <option value="9">AAA_diameter</option>
    </select>
    </form>
    
    
    <!--search bar for search term input -->
    <form name ="search_form" method="POST" action="VGR_data_display.php">
    
        <input id="search_box" type="text" name="search_box" value="" />
        <input id="submit" type ="submit" name ="search" value ="Ok">
    
    </form>
    
    <table style="margin:auto;" id="table" border='1'>
    <tr>
    <th>ID</th>
    <th>F_ar</th>
    <th>Postnr</th>
    <th>Postort</th>
    <th>Vardgivare</th>
    <th>Team</th>
    <th>Orsak</th>
    <th>Planerat_datum</th>
    <th>fran</th>
    <th>AAA_diam</th>
    </tr>
    
    
    <?php while($row = mysql_fetch_array($query)) { ?>
      <tr>
      <td><?php echo $row['id']; ?></td>
      <td><?php echo $row['F_ar']; ?></td>
      <td><?php echo $row['Postnr']; ?></td>
      <td><?php echo $row['Postort']; ?></td>
      <td><?php echo $row['Vardgivare']; ?></td>
      <td><?php echo $row['Team']; ?></td>
      <td><?php echo $row['Orsak']; ?></td>
      <td><?php echo $row['Planerat_datum']; ?></td>
      <td><?php echo $row['fran']; ?></td>
      <td><?php echo $row['AAA_diam']; ?></td>
      </tr>
    
    <?php } ?>
    

2 个答案:

答案 0 :(得分:0)

更改下拉列表,以便选择值是数据库列名。即

<select id="dropdown" name="filter">
  <option value=""></option>
  <option value="ID">ID</option>
</select>

然后在PHP中:

if (isset($_POST['search'])) {

    $search_term = mysql_real_escape_string($_POST['search_box']);
    $column = mysql_real_escape_string($_POST['filter']);
}

SQL

SELECT * FROM mytable where $column = '$search_term'

这意味着添加新选项很容易,并且保存了一个冗长的if / switch语句。它还允许您在线下构建多个选项。

注意:如果您担心在表单中显示列名,可以在构建下拉列表时加密它们,然后在需要使用它们时解密

编辑 - 基于评论。

在SQL之前:

$allowed[] = array("ID") //Add all allowed columns.
if (in_array($column, $allowed))
{
  //Run SQL
}

如果希望自动化,可以在构建DDL时构建此数组。

答案 1 :(得分:-1)

如果我已正确理解,那么您只想搜索下拉列表中选择的列。然后你想用这个:

if (isset($_POST['search'])) {

    $search_term = mysql_real_escape_string($_POST['search_box']);

    switch($_POST['filter']) {

        Default: 
            $sql .= " WHERE F_ar = '$search_term' ";
            break;
        case 1:
            $sql .= " WHERE Postnr = '$search_term' ";
            break;
        case 2:
            $sql .= " WHERE Postort = '$search_term' ";
            break;
        case 3:
            $sql .= " WHERE Vardgivare = '$search_term' ";
            break;
        case 4:
            $sql .= " WHERE Team = '$search_term' ";
            break;
        case 5:
            $sql .= " WHERE Orsak = '$search_term' ";
            break;
        case 6:
            $sql .= " WHERE Planerat_datum = '$search_term' ";
            break;
        case 7:
            $sql .= " WHERE fran = '$search_term' ";
            break;
        case 8:
            $sql .= " WHERE AAA_diam = '$search_term'; ";
            break;

    }
}

<小时/> 至于你的错误,你有两种形式,你需要一切形式如下:

<form name="Select_filter" method="POST" action="VGR_data_display.php">
    <select id="dropdown" name="filter">
        <option value=""></option>
        <option value="1">ID</option>
        <option value="2">Alder</option>
        <option value="3">Postnummer</option>
        <option value="5">Postort</option>
        <option value="6">Vårdgivare</option>
        <option value="7">Planerat Datum</option>
        <option value="8">Status</option>
        <option value="9">AAA_diameter</option>
    </select>


    <!--search bar for search term input -->

    <input id="search_box" type="text" name="search_box" value="" />
    <input id="submit" type ="submit" name ="search" value ="Ok">

</form>