dongqian1925 2016-02-16 10:19
浏览 16
已采纳

从表中查询值

I am trying to get values from table the problem I am facing is that regardless of what the input is it gives me the result of both rows. I want to match the input ID with the ID in the table. This is the code I am trying to useImage of the table is attached the file name is linked with the javascript file where I have a click function and this javascript(global.js) file is linked with another file having text box. My main file code is below

  <!DOCTYPE html>
<html>
    <head>
        <title>products</title>
    </head>
    <body>
            Item: 
            <input type="text" required id="item">
            <input type="submit" id="item-submit" value="Grab"> 
            <div id="item-data"></div>

            <script src="http://code.jquery.com/jquery-2.2.0.min.js"></script> 
            <script src="js/global.js"></script> 

    </body>
</html>

global.js file code

 $(document).ready(function(){
    $('input#item-submit').click(function(){     

        var item = $('input#item').val();    
        if ($.trim(item) != ''){             
            $.post('ajax/name.php',{item:name}, function(data){ 
                $('div#item-data').text(data);  

            });

        }

    })
});

name.php file code is below

 require '../db/connect.php';

    $sql = "SELECT BarcodeID, shirts, price FROM clothes WHERE BarcodeID = ($_POST['name']) ";

    $result = mysqli_query($con,$sql);
        while($row = mysqli_fetch_array($result)) {
            echo " " . $row["BarcodeID"]. " shirt color:  " . $row["shirts"]. " price: " . $row["price"];
        }
    mysqli_close($con);
  • 写回答

2条回答 默认 最新

  • douzuo5504 2016-02-16 10:29
    关注
        $id=$_POST['item']; // your post variable
        $sql = "SELECT BarcodeID, shirts, price FROM clothes WHERE BarcodeID=".$id;
    
         $result = mysqli_query($con,$sql);
    if(mysqli_num_rows($result)>0)
    {
            while($row = mysqli_fetch_array($result)) {
                echo " " . $row["BarcodeID"]. " shirt color:  " . $row["shirts"]. " price: " . $row["price"];
            }
    }
    else
    {
    echo "No results found";
    }
        mysqli_close($con);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比