douqiang7976 2015-11-24 18:43
浏览 37
已采纳

使用下拉框过滤我的postgreSQL表

The Problem

I am having trouble filtering my table, no errors are coming up so I am unsure why it isn't working. I am wondering whether it is to do with the placement of SQL code or whether the submit button isn't coded correctly... Or even if the ive programmed it all wrong haha.

Expected Outcome

What I expect to see when press submit is for the table to change to what ever it was that submitted. For exmple, in the drop down box there is an option for 'Canned' food, if I was to press this I would expect to see the table change, showing only canned food.

What the actual outcome is

Nothing seems to be happening, and with no errors, i'm completely unsure as to why.

Here is the code for the drop down box

I am unsure whether this is the correct form for what I am trying to do. I want to stay on the same page, I only want to affect the table.

<form action="database.php">
<select name="category" id="category">
    <option value="Alcoholic">Alcohol</option>
    <option value="Canned">Canned Food</option>
    <option value="Dairy">Dairy</option>
    <option value="Dessert">Dessert</option>
    <option value="Frozen">Frozen Food</option>
    <option value="Fruit">Fruit</option>
    <option value="Junk Food">Junk Food</option>  
</select>
<input  type="submit" name="submit" value="Search"/>
</form>

Here is the code for the table and the SQL commands

<?php
    $conn = pg_connect("host=db.dcs.aber.ac.uk port=5432
                                        dbname=teaching user=csguest password=********");
// Empty var that will be populated if the form is submitted
    $where = '';

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

         if (!empty($_POST['category'])) {
             // Where conditional that will be used in the SQL query
             $where = " WHERE Category = '".pg_escape_string($_POST['category'])."'";
    }

}

$res = pg_query($conn, "SELECT Foodtype, Manufacturer, Description, Price 
                                    FROM food " . $where . " ORDER BY    Category ASC");
echo "<table id=\"myTable\" border='1'>";
while ($a = pg_fetch_row($res)) {
    echo "<tr>";
    for ($j = 0; $j < pg_num_fields($res); $j++) {
        echo "<td>" . $a[$j] . "</td>";
    }

    echo "<td><form id='cart' name='cart' method='POST' action='addToBasket.php'>
                                        <input type='submit' name='Select' id='Select' value='Add To Basket'>
                                        </form></td>";
    echo "</tr>
";
}

echo "</table>
";
$Alcoholic = pg_query("SELECT Foodtype, Manufacturer, 
                                    Description, Price FROM food WHERE Category = 'Alcoholic'");
$Canned = pg_query("SELECT Foodtype, Manufacturer, 
                                    Description, Price FROM food WHERE Category = 'Canned'");
?>

The last two SQL statements above are supposed to filter the table, there will one for each of the drop down options

  • 写回答

1条回答 默认 最新

  • dounuo7954 2015-11-24 19:16
    关注

    Since the discussion on the comments lead to the solution, I'm adding it here:

    Your problem is that you defined a form without a method which means that it will be by default GET and in your code you are trying to get POST variables so change your form to:

    <form action="database.php" method="post">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog