简体   繁体   English

如何获取在php中检查了哪些清单项并使用它们进行sql查询?

[英]How can I obtain which checklist items are checked in php and use them to make a sql query?

I'm trying to create a checklist that acts as a filter for my database. 我正在尝试创建一个清单,作为我的数据库的过滤器。 The checklist would be use to enter multiple values to search for. 该清单将用于输入要搜索的多个值。 For example, the user should be able to search for the brands AC and Abarth at the same time. 例如,用户应该能够同时搜索AC和Abarth品牌。

This is what the checklist for brands looks like. 这就是品牌清单的样子。

<h3>Brand</h3>
                <?php 
                    $query = "select distinct(Brand) from Sheet1 order by Brand ASC";  
                    $rs = mysqli_query($conn,$query) or die("Error : ".mysql_error());
                    while($brand_data = mysqli_fetch_assoc($rs)){

                ?>
                    <a href="javascript:void(0);" class="list-group-item"> 
                    <input type="checkbox" class="item_filter brand" value="<?php echo $brand_data['Brand']; ?>"  >
                    &nbsp;&nbsp; <?php echo $brand_data['Brand']; ?></a>
                <?php } ?>

My ajax code. 我的ajax代码。

var brand;
$(function(){
    $('.item_filter').click(function(){
        $('.product-data').html('<div id="loaderpro" style="" ></div>');

         brand = multiple_values('brand');

        $.ajax({
            url:"ajax.php",
            type:'post',
            data:{brand:brand},
            success:function(result){
                $('.product-data').html(result);
            }
        });
    });

});


function multiple_values(inputclass){
    var val = new Array();
    $("."+inputclass+":checked").each(function() {
        val.push($(this).val());
    });
    return val;
}

This is how my php handles the checkbox. 这就是我的php处理复选框的方式。

$brand = isset($_REQUEST['Brand'])?$_REQUEST['Brand']:"";
$query = "select * from Sheet1 where"; 

                   //filter query start 
                      if(!empty($brand)){
                          $branddata =implode("','",$brand);
                          $query  .= " Brand in('$branddata')"; 
                      }
$rs = mysqli_query($conn,$query) or die("Error : ".mysqli_error($conn))

The result should be $query= select * from Sheet1 where Brand in ('AC', 'Abarth'). 结果应为$ query = select * from Sheet1,其中Brand in('AC','Abarth')。 This is for my database to show the all car models with the brand AC or Abarth. 这是我的数据库,其中显示了AC或Abarth品牌的所有汽车型号。 The current result I'm getting is that it won't give a value of what is checked in the checklist. 我得到的当前结果是,它不会提供清单中所检查内容的值。 So the final result is nothing, it might be because of my ajax code, because $brand doesn't get any value. 所以最终结果什么都没有,可能是因为我的Ajax代码,因为$ brand没有获得任何价值。 The value should be an array that is later converted into a single string. 该值应该是一个数组,该数组以后将转换为单个字符串。

Figured out my own problem. 弄清楚我自己的问题。 The php was wrong. PHP是错误的。 The php is looking for values for the variable Brand, meanwhile the ajax is sending values to the variable brand. php正在寻找变量Brand的值,而ajax会将值发送到变量brand。

Fixed by changing 通过更改固定

$brand = isset($_REQUEST['Brand'])?$_REQUEST['Brand']:"";

to

$brand = isset($_REQUEST['brand'])?$_REQUEST['brand']:"";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (PHP)数组中的键值对数目未知,如何在sql查询中使用它们? - (PHP) I have an unknown number of key value pairs in an array, how can I use them in an sql query? PHP中的SQL查询,我可以将它们组合吗? - SQL QUERY IN PHP, can i combine them? 如何使此SQL查询更有效? PHP - How can I make this SQL query more efficient? PHP 如何在 Laravel 中同时使用“paginate ()”和清单? - How can I use “paginate ()” and checklist at the same time with Laravel? 如何查看 PHP 中选中了哪些复选框? - How can I see which check boxes are checked in PHP? PHP Todo CheckList检查的功能 - PHP Todo CheckList Checked Feature 如何获取表中存储的SQL查询并为表中的所有查询创建动态PHP页面 - How can retrieve sql query which stored in table and make dynamic php page for all queries in table php:我有5个数组,我怎么能把它们作为一个? - php: i have 5 array, how can i make them as one? 如何进行SQL查询,以选择与某个字符串匹配的所有行,然后根据匹配数对它们进行加权 - How can I make a SQL query that selects all rows matching a certain string then weights them based on the number of matches 使用PHP Mysql,如何从sql查询中的多个OR条件中满足哪个条件? - Using PHP Mysql how can I get which condition satisfied from multiple OR condition in a sql query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM