Reputation: 27
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);
Upvotes: 0
Views: 77
Reputation: 1390
$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);
Upvotes: 0
Reputation: 3515
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?.
Upvotes: 1