douzhi9478 2014-12-02 12:11
浏览 33

在Dreamweaver中过滤Php查询

I have to a filter search criteria that sllows customers to filter by price availabilty and category. When i try to filter by Price it displays just the number and not the products that are that price. I was wondering if anyone can help me what i did wrong.

this is my form

<form action="results2.php" method="post">
    Name  <input type="text" name="price" >
    <input type="submit" name="search" value="Find Me">
</form>

and this is my results2.php

<?php
if (isset($_POST['search'])) {
    $get_name = $_POST['price'];
    echo $get_name;
    $query = "SELECT * FROM product WHERE price LIKE '%$get_name%'";
    $result = mysqli_query($connection, $query);
    while ($row = mysqli_fetch_array($result)) {
        $productName = $row['productName'];
        $description = $row['description'];
        $category = $row['category'];
        $availibilty = $row['availibilty'];
        $price = $row['price'];
        $height = $row['height'];
        echo $productName . " " . $description . "  " . $category . " " . $availibilty . " " . $price . " " . $height . "<br />";
    }
}
?>

I am trying to filter by price when it does it just displays the Price and not all my products

  • 写回答

1条回答 默认 最新

  • douchui4459 2014-12-02 17:09
    关注

    The way you must be doing this is using PDO.

    $dsn = 'mysql:dbname=testdb;host=127.0.0.1';
    $user = 'dbuser';
    $password = 'dbpass';
    
    $dbh = new PDO($dsn, $user, $password);
    
    $stmt = $dbh->prepare('
        SELECT
            *
        FROM product
        WHERE
            price >= :low
    ');
    
    $stmt->bindParam(':low', $_POST['price']);
    
    $stmt->execute();
    // you can test the return value
    
    while(($result = $stmt->fetch(PDO::FETCH_OBJ) !== false)
    {
        // output the records
    }
    

    I wrote this inline so, most probably it wont work out of the box but it introduces you to PDO, Prepared Statements and how to address SQL INJECTION.

    You're also encourage to filter and sanitize $_POST. Have a look on PHP filter_var functions

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集