Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax mysql php筛选器无法与mysqli函数一起使用_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Ajax mysql php筛选器无法与mysqli函数一起使用

Ajax mysql php筛选器无法与mysqli函数一起使用,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我使用的是ajax、jquery、mysql和php过滤器,它可以与PDO连接一起正常工作,但是当我使用MYSQLI连接时,它不工作,并且不显示任何输出,我的html和ajax代码是 index.php <table id="phones"> <thead> <tr> <!-- <th width="15">ID</th>--> <th>B

我使用的是ajax、jquery、mysql和php过滤器,它可以与PDO连接一起正常工作,但是当我使用MYSQLI连接时,它不工作,并且不显示任何输出,我的html和ajax代码是

index.php

<table id="phones">
    <thead>
        <tr>
            <!--  <th width="15">ID</th>-->
            <th>Brand</th>
            <th>Model</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<div id="filter">
    <h2>Filter options</h2>
    <div>
        <input type="checkbox" id="Samsung">
        <label for="Samsung">Samsung</label>
    </div>
    <div>
        <input type="checkbox" id="iPhone">
        <label for="iPhone">iPhone</label>
    </div>
    <div>
        <input type="checkbox" id="HTC">
        <label for="HTC">HTC</label>
    </div>
    <div>
        <input type="checkbox" id="LG">
        <label for="LG">LG</label>
    </div>
    <div>
        <input type="checkbox" id="Nokia">
        <label for="Nokia">Nokia</label>
    </div>
</div>

<script src="jquery-latest.js"></script> 
<script>
    function makeTable(data){
        var tbl_body = "";
        $.each(data, function() {
            var tbl_row = "",
            currRecord = this;

            $.each(this, function(k , v) {
                if(k==='model'){
                    v = "<a href='content.php?id=" + currRecord['model'] +"'>" + v + "</a>";
                } else if (k==='price'){
                    v = "<span class='price'>" + v + "</span>";
                }
                tbl_row += "<td>"+ v +"</td>";
            });
            tbl_body += "<tr>"+tbl_row+"</tr>";
        });

        return tbl_body;
    }

    function getPhoneFilterOptions(){
        var opts = [];
        $checkboxes.each(function(){
          if(this.checked){
            opts.push(this.id);
          }
        });

        return opts;
    }

    function updatePhones(opts){
        if(!opts || !opts.length){
            opts = allBrands;
        }

        $.ajax({
          type: "POST",
          url: "submit.php",
          dataType : 'json',
          cache: false,
          data: {filterOpts: opts},
          success: function(records){
            $('#phones tbody').html(makeTable(records));
            updatePrices();
          }
        });
    }

    var $checkboxes = $("input:checkbox");
    $checkboxes.on("change", function(){
        var opts = getPhoneFilterOptions();
        updatePhones(opts);
    });


    var allBrands = [];
    $("input[type=checkbox]").each(function(){
        allBrands.push($(this)[0].id)
    })

    updatePhones();
    updatePrices();
</script>

烙印
模型
价格
过滤器选项
三星
苹果手机
宏达电
LG
诺基亚
函数makeTable(数据){
var tbl_body=“”;
$.each(数据,函数(){
变量tbl_行=”,
currRecord=这个;
$。每个(此,函数(k,v){
如果(k=='model'){
v=“”;
}else if(k=='price'){
v=“”+v+”;
}
tbl_行+=“+v+”;
});
tbl_正文+=“”+tbl_行+“”;
});
返回tbl_体;
}
函数getPhoneFilterOptions(){
var opts=[];
$复选框。每个(函数(){
如果(选中此项){
选择推送(this.id);
}
});
返回选项;
}
函数更新电话(opts){
如果(!opts | |!opts.length){
opts=所有品牌;
}
$.ajax({
类型:“POST”,
url:“submit.php”,
数据类型:“json”,
cache:false,
数据:{filterOpts:opts},
成功:功能(记录){
$('#phones tbody').html(makeTable(records));
updatePrices();
}
});
}
var$复选框=$(“输入:复选框”);
$checkbox.on(“更改”,函数(){
var opts=getPhoneFilterOptions();
更新电话(opts);
});
var allBrands=[];
$(“输入[type=checkbox]”)。每个(函数(){
allBrands.push($(此)[0].id)
})
更新电话();
updatePrices();
submit.php(MYSQLI连接)


submit.php(PDO连接)



提前感谢

正如我在评论中所说的,您不会将数据放在MySQLi prepared语句的占位符中:

$opts = isset($_POST['filterOpts']) ? $_POST['filterOpts'] : '';
$qMarks = str_repeat('?,', count($opts) - 1) . '?';

$sql="SELECT name, model, price  FROM mobile_phone_name WHERE name IN ($qMarks)";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_assoc($result))
哪里使用了
$opts

其工作原理如下(未经测试):

类型不匹配:

$opts = isset($_POST['filterOpts']) ? $_POST['filterOpts'] : '';
一定是

$opts = isset($_POST['filterOpts']) ? $_POST['filterOpts'] : array();

您可以随时启用错误报告或查看developers/javascript控制台,该控制台将返回有用的信息。“不起作用”-找出错误是您的工作,我们在这里帮助您解决问题。您的mysql工具可以独立工作吗?你能运行PHP页面吗?亲爱的@DanFromGermany谢谢你的回复,在这里我没有收到任何错误,所以我在这里发布我的问题,因为我在几个小时前就被困在这个问题中,我大部分时间都试图解决它,但最后我没有得到任何解决方案,所以我向你询问,并为我的英语不好感到抱歉(@JayBlanchard ya我可以运行PHP页面,所以我在这里说我的PDO连接正常。但是当我尝试使用MYSQLI连接时,发现它不正常。你能帮我吗?你在其中放置了占位符
,但不给它提供数据。
echo$sql;
-sql语句也错了,缺少空格等等。谢谢你的支持eply@DanFromGermany,你是对的,我忘了添加$opts,但我不知道我应该在哪里使用$opts。你能建议我应该在哪里使用$opts吗?谢谢,你的回复将不胜感激。我确实在回答中建议了如何使用它。
$stmt = $con->prepare($sql);
foreach ($opts as $v) {
    $stmt->bind_param('s', $v);
}
$stmt->execute();

while($row = $stmt->fetch())
$opts = isset($_POST['filterOpts']) ? $_POST['filterOpts'] : '';
$opts = isset($_POST['filterOpts']) ? $_POST['filterOpts'] : array();