gpt4 book ai didi

php - MySQL 查询不只输出结果标题

转载 作者:行者123 更新时间:2023-11-28 16:23:52 25 4
gpt4 key购买 nike

我目前正在练习 PHP 和 AJAX,我正在尝试创建一个允许用户实时搜索数据库以查找产品库存的应用程序。当用户在其中一个条目中键入一个字母时,会显示标题但不会显示数据。我不确定我是否做错了,但我会让你们来判断。代码如下:

Results.php - Below

 <!DOCTYPE html>
<html>
<head>
<title>Results</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<?php
$q = intval($_GET['q']);

$con = mysqli_connect("localhost", "username", "password", "databaseName");
//edited out for safety

if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"jakeholc_ajaxtest");

$sql="SELECT * FROM stock WHERE name LIKE '%{$q}%'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Product name</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
</tr>";

if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['descr'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "</tr>";
}
}


echo "</table>";
mysqli_close($con);
?>

最佳答案

$q = intval($_GET['q']); 尝试将您的输入转换为整数。删除此行。

关于php - MySQL 查询不只输出结果标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47705793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com