mysqli查询在foreach php中不起作用


mysqli query not working in foreach php

我现在的问题是,当我点击页面上的图片时,它将第一次显示出来。但它将第二次显示失败。这个过程将从将数据发送到ajax开始,然后ajax(prosec.js)将其发送到php页面(process1.php)

当我删除blockquote($query="SELECT…")中的代码时,它将运行,但如果不运行,它将显示失败。

process1.php

<?php
    include 'session.php';
    include 'connection.php';
    if(isset($_POST['dataS'])) {
        $table = $_POST['table'];
        $concat = "";
        $serial = $_POST['dataS'];
        $query = "SELECT * FROM product WHERE serialNum = '$serial'";
        $result = mysqli_query($conn, $query);
        $row = mysqli_fetch_assoc($result);
        if($row) {
            $prodName = $row['prodName'];
            $quanProd = 1;
            $priceProd = $_POST['total'] + $row['salePrice'];
            if($table == "") {
                $query = "SELECT * FROM product WHERE serialNum = '$serial'";
                $result = mysqli_query($conn, $query);
                $row = mysqli_fetch_assoc($result);
            }
            else{
                $DOM = new DOMDocument;
                $DOM->loadHTML($table);
                $items = $DOM->getElementsByTagName('tr');
                $check = 0;
                $check_one = 0;
                $y=0;
                function tdrows($elements,$check,$serial,$prodName,$y) { 
                    $quantity="";
                    $item = "";
                    $price = "";
                    $delete = "";
                    $x = 0;
                    foreach($elements as $element) { 
                        if($x == 0)
                            $delete = $element->nodeValue;
                        else if($x == 1)
                            $item = $element->nodeValue;
                        else if($x == 2)
                            $quantity = $element->nodeValue;
                        else if($x == 3)
                            $price = $element->nodeValue;
                        $x++;
                    }
                    **$query = 'SELECT prodName FROM product WHERE prodName = "$item"';
                    $search = mysqli_query($conn, $query) or die(mysqli_error()); 
                    $row = mysqli_fetch_assoc($search);
                    $s = $row['prodName'];**
                    if($prodName == $s) {
                        $quantity++;
                        $check = 1;
                    }
                    else {
                        $check = 0;
                    }
                    return $check;
                }

                foreach ($items as $node) {
                    $check = tdrows($node->childNodes,$check,$serial,$prodName,$y);
                    $y++;
                }
            }
            $priceProd = number_format((float)$priceProd, 2, '.', ''); 
            echo json_encode (
                array ( //this array is used to send the data back to ajax.
                    "success" => "1",
                    "concat" => $concat,
                    "quantity" => $quanProd,
                    "price" => $priceProd,
                )
            );
        }
        else {
            echo json_encode (
                array ( //this array is used to send the data back to ajax.
                    "success" => "0",
                )
            );
        }
    }
    ?>

process.js

$(document).ready(
    function() {
        $("body").on("click","#product .add",
            function(e) {
                var total = document.getElementById("total").value;
                var table = document.getElementById('table-list').innerHTML;
                table = (table.trim) ? table.trim() : table.replace(/^'s+/,'');
                var serial = $(this).attr('id');
                var totalQ = document.getElementById("totalQ").value;
                if(total == "")
                    total = 0;
                else
                    total = parseFloat(total);
                if(totalQ == "")
                    totalQ = 0;
                else
                    totalQ = parseInt(totalQ);  
                var dataS = serial;
                e.preventDefault();
                $.ajax({
                    type : "POST",
                    url : "process1.php",
                    crossDomain: true,
                    data : {dataS : dataS, table : table, total : total},
                    dataType : 'json',  
                })
                .done(function(html) {
                    if(html.success == 1) {
                        console.log('done: %o', html);  
                        $("#table-list").html(html.concat).show();
                        document.getElementById('totalQuantity').innerHTML = html.quantity;
                        document.getElementById("total").value = html.price;
                        document.getElementById("payment").value = html.price;
                        document.getElementById('totalQ').value = html.quantity;
                        document.getElementById('title').innerHTML = html.price;
                        document.getElementById('input').value='';
                        $("#input").focus();
                    }
                    else {
                        alert("Wrong serial number!");
                        document.getElementById('input').value='';
                        $("#input").focus();
                    }
                })
                .fail(function(html) {
                    console.info('fail: %o', html);  
                    alert("fail");
                });
                return false;
        });
}); 

connection.php

<?php
    $conn = mysqli_connect('localhost','root','','rds');
?>

您的查询错误:请尝试此

$query = "SELECT prodName FROM product WHERE prodName = '".$item."'";

根据您的图片,您的问题是数据库连接不正确。当您执行第一个请求时,它不会进行任何数据库交互(因为在blocknotes之外)。您将发送table数据的第二个请求将执行查询。因此,第一个请求将成功,而第二个请求将在mysqli$conn)对象上给您一个错误。

if($table == "") {
    //Database interaction
    $query = "SELECT * FROM product WHERE serialNum = '$serial'";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_assoc($result);
}
else{
    //No database interaction because of the blocknotes
    $DOM = new DOMDocument;
    $DOM->loadHTML($table);
    $items = $DOM->getElementsByTagName('tr');
    $check = 0;
    $check_one = 0;
    $y=0;
    function tdrows($elements,$check,$serial,$prodName,$y) { 
        $quantity="";
        $item = "";
        $price = "";
        $delete = "";
        $x = 0;
        foreach($elements as $element) { 
            if($x == 0)
                $delete = $element->nodeValue;
            else if($x == 1)
                $item = $element->nodeValue;
            else if($x == 2)
                $quantity = $element->nodeValue;
            else if($x == 3)
                $price = $element->nodeValue;
            $x++;
        }
        **$query = 'SELECT prodName FROM product WHERE prodName = "$item"';
        $search = mysqli_query($conn, $query) or die(mysqli_error()); 
        $row = mysqli_fetch_assoc($search);
        $s = $row['prodName'];**
        if($prodName == $s) {
            $quantity++;
            $check = 1;
        }
        else {
            $check = 0;
        }
        return $check;
    }

    foreach ($items as $node) {
        $check = tdrows($node->childNodes,$check,$serial,$prodName,$y);
        $y++;
    }
}

请检查您的用户名、密码和数据库名称。我敢肯定你在这里用错了什么。如connection.php文件中所述,您不使用密码。您确定用户root没有密码吗?你能用phpMyAdmin这样的MySQL管理工具访问数据库吗?