无法通过AJAX调用从mysql获取数据

时间:2022-12-03 14:28:06

I'm having trouble trying to get data from a database. The code will only retrieve the first row of data from the Database. The data is correct but it does not matter what information I put into my html input field the data is the same in the new row that is added. Any help resolving this would be fantastic.

我在尝试从数据库中获取数据时遇到了麻烦。代码只会从数据库中检索第一行数据。数据是正确的,但是我在html输入字段中输入的信息与添加的新行中的数据相同并不重要。解决这个问题的任何帮助都会很棒。

If someone was going to point out that I have both "POST" and "GET" mixed in my code, I could understand as that being a possible problem, but neither when matching would retrieve data from the DB, other than the first row of data in the table.

如果有人要指出我在我的代码中混合了“POST”和“GET”,我可以理解这是一个可能的问题,但是当匹配将从DB检索数据时,除了第一行之外表中的数据。

Any help with this would be great!!! Thanks to all who provide an answer in advance.

任何帮助都会很棒!!!感谢所有提前回答的人。

My HTML code:

我的HTML代码:

index.php

    <div class="row">
        <div class="col-md-1"></div>
            <div class="col-xs-3">
            <h3 class="h4 text-center"><input type="text" name="barcode" id="barcode" size="90" class="col-md-9" value="" method="GET" placeholder="Barcode / Product Name"></h3>

             </div>
        </div>
    <br />
    <div class="row">
        <div class="col-md-1"><p class=""></p></div>
        <div class="col-md-6">
            <table id="report" class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <td>SKU</td>
                        <td>Model</td>
                        <td>Item Description</td>
                        <td>Qty</td>
                    </tr>
                </thead>
                <tbody>
                 <?php get_item(); ?>
                </tbody>
            </table>
       </div>
    </div>

This is my AJAX script

这是我的AJAX脚本

<script>
var inp = $("#barcode");
// where #txt is the id of the textbox

$("#barcode").keyup(function (event) {
if (event.keyCode == 13)
{
    if (inp.val().length > 0)
    {
        $.ajax({
            url: "index.php",
            type: "GET", //Also tried POST method. Didn't work either
            data: {id: inp.val()},
            success: function(response)
            {
                values = response.split(' - ');
                $('#report tr:last').after(
                    "<tr class='table-row'>" +
                    "<td class=''>" + values[1] + "</td>" +
                    "<td class=''>" + values[2] + "</td>" +
                    "<td class=''>" + values[3] + "</td>" +
                    "<td class=''>" + values[4] + "</td></tr>");
            }});
    }
    $('input[name=barcode]').val('');
    }

 });
</script>

Here is my php code

这是我的PHP代码

function get_item(){
global $con;    
if(!empty($_POST))
{
    $query = query("SELECT * FROM items");
        confirm($query);
    while($row = fetch_array($query)) {
        $sku = $row['sku'];
        $model = $row['category'];
        $desc = $row['description'];
        $qty = $row['qty'];

    echo($id.' - '.$sku.' - '.$model.' - '.$desc.' - '.$qty);
    die();
    }
}
}

1 个解决方案

#1


2  

Move the die(); function call out of your while loop. It gets called rigth at the end of the first loop terminating your php script.

移动骰子();函数调出你的while循环。它在第一个循环结束时被称为rigth,终止你的php脚本。

#1


2  

Move the die(); function call out of your while loop. It gets called rigth at the end of the first loop terminating your php script.

移动骰子();函数调出你的while循环。它在第一个循环结束时被称为rigth,终止你的php脚本。