我正在尝试为我的汽车网站过滤汽车品牌和燃料但它不会过滤结果谁能帮我?

问题描述

我正在尝试使用 AJAX 和 PHP 准备语句过滤一些记录。目前查询没有返回任何结果。我认为这是因为查询本身,但我不确定。错误是否因为传递的值而发生?有人能帮我解决这个问题吗?

    if(isset($_POST['action']))
    {
        
        $sql = "SELECT * FROM occasions WHERE naam IS NOT NULL";
        
        if(isset($_POST['merk'])){
            $sql .= " AND merk IN(:merk)";
            
            $merk = implode(",",$_POST['merk']);
        }
        
        if(isset($_POST['brandstof'])){
            $sql .= " AND brandstof IN(:brandstof)";
            
            $brandstof = implode(",$_POST['brandstof']);
        }

这里的问题是:查询过滤了一次,当输入第二个 $merk 值时,它不会过滤。这是为什么?与 $brandstof 相同。

        //We prepare our SELECT statement.
        $statement = $pdo->prepare($sql);
        
        
        
        
        if(isset($_POST['merk'])){
            $statement->bindParam(':merk',$merk);
        }

        if(isset($_POST['brandstof'])){
            $statement->bindParam(':brandstof',$brandstof);
        }

这些参数是否正确绑定?

        //Execute statement.
        $statement->execute();
        
        print_r($statement);

        //Fetch the result.
        $results = $statement->fetchAll(PDO::FETCH_ASSOC);

        $output = "";
        
        if(count($results) > 0)
        {
            foreach($results as $occasion)
            {
                $output .= 
                    '<a href="../show/"'.$occasion['id'].'" " style="width: 33%; text-align: center;">
                        <div style="width:300px; display:inline-block;">
                            <div class="card">
                                <div class="occasion-img">
                                    <img class="card-img-top" src="/uploads/'.$occasion['foto'].'" alt=""'.$occasion['naam'].'"" title=""'.$occasion['naam'].'"" style="width:100%; height:270px; object-fit:cover;">
                                </div>

                                <div class="occasion-tekst" style="padding:10px 10px;">
                                    <h2>'.$occasion['naam'].'</h2>
                                    <p style="line-height:1.0;">
                                        <?PHP echo $beschrijving; ?>
                                    </p>
                                    <h4 style="text-align:center;">
                                        &#8364; '.number_format($occasion['prijs'],2,"").'
                                    </h4>
                                </div>
                            </div>
                        </div>
                    </a>';
            }
        }
        else
        {
            $output = "<h3>Er zijn geen producten gevonden</h3>" . $merk;
        }
        echo $output;
        echo $sql;
    }

如果找到结果,则输出为 HTML。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)