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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助