我试图从表中获取值,我面临的问题是,无论输入是什么,它都会给我两行的结果。我想将输入 ID 与表中的 ID 进行匹配。这是我尝试使用的代码Image of the table is attached文件名与 javascript 文件链接,其中我有一个单击功能,并且此 javascript(global.js) 文件与另一个具有文本框的文件链接。 我的主要文件代码如下
<!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文件代码
$(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文件代码如下
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);
最佳答案
尝试这个查询:
"SELECT BarcodeID, shirts, price FROM clothes WHERE BarcodeID = '{$_POST['name']}'"
为什么要将 BarcodeID
与名称进行比较?
Side note : Your query is unsafe. Read this How can I prevent SQL injection in PHP?.
关于javascript - 从表中查询值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35429755/