0

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 Answers2

1

Try this query :

"SELECT BarcodeID, shirts, price FROM clothes WHERE BarcodeID = '{$_POST['name']}'"

And why are you comparing BarcodeID with name?

Side note : Your query is unsafe. Read this How can I prevent SQL injection in PHP?.

Community
  • 1
  • 1
Mr. Engineer
  • 3,522
  • 4
  • 17
  • 34
  • Below is the javascript file this global.js file is linked with my main file with following code for text box and submit button .js file -> $(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); }); } }) }); – hammad rashid Feb 16 '16 at 11:24
  • Please don't do like this. Don't dump your code here. Please edit your question. – Mr. Engineer Feb 16 '16 at 11:25
  • Just one thing. If you are using like this : `{item:name}` then you need to use `$_POST['item']` not `$_POST['name']`. – Mr. Engineer Feb 16 '16 at 11:41
  • ohh, Is rest of the code fine ?. Item is the variable carrying the data name right? – hammad rashid Feb 16 '16 at 11:51
  • Yes right.Please accept my answer if it's helpful to you. – Mr. Engineer Feb 16 '16 at 11:52
  • Its still giving me error. Parse error: syntax error, unexpected '$result' (T_VARIABLE) in C:\wamp\www\ajax\name.php on line 41 Line 41 is this $result = mysqli_query($con,$sql); – hammad rashid Feb 16 '16 at 11:55
  • I think you are missing semicolon (`;`) on that line. – Mr. Engineer Feb 16 '16 at 12:02
  • I do have a semicolon. – hammad rashid Feb 16 '16 at 12:29
0
    $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);
safin chacko
  • 1,345
  • 1
  • 11
  • 18
  • These are the errors I got. Line 40 is $id=$_POST['name']; and line 43 is while($row = mysqli_fetch_array($result)) Notice: Undefined index: name in C:\wamp\www\ajax\name.php on line 40 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\ajax\name.php on line 43 – hammad rashid Feb 16 '16 at 11:48
  • Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\ajax\name.php on line 44 Line 44 is if(mysqli_num_rows($result)>0) – hammad rashid Feb 16 '16 at 12:34
  • Test the query by giving direct value 1 as $id. Then Note the result. ie $sql = "SELECT BarcodeID, shirts, price FROM clothes WHERE BarcodeID=1"; – safin chacko Feb 16 '16 at 12:47
  • Yes this is working correctly. This means my name.php file is not getting the input. So the issue can be in my global.js file. – hammad rashid Feb 16 '16 at 13:05
  • ok. you can check the POST is working or not by using echo $_POST['item']; exit(); – safin chacko Feb 16 '16 at 13:11
  • Basically I should change my this line echo " " . $row["BarcodeID"]. " shirt color: " . $row["shirts"]. " price: " . $row["price"]; to echo $_POST['item']; exit(); It gives me the same error Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\ajax\name.php on line 44 – hammad rashid Feb 16 '16 at 13:26
  • echo $_POST['item']; exit(); give this top of the page and check. – safin chacko Feb 16 '16 at 13:30
  • ie nothing POST receives . error in global.js in {item:name} put {id:item} and use echo $_POST['id']; exit(); – safin chacko Feb 16 '16 at 13:40
  • It worked. Tried in the full code. Now I am getting the proper result. – hammad rashid Feb 16 '16 at 13:52