【发布时间】:2019-08-20 09:22:14
【问题描述】:
我在页面上有搜索功能,但我需要使用准备好的语句。尝试使用时,它会不断发回 0 个结果。
这里是搜索栏所在的页面。
<form action="search.php" method="post">
<input type="text" name="search" placeholder="Value To Search">
<button type="submit" name="search">
</button>
</form>
这是 search.php 页面
<?php
$db = mysqli_connect('localhost', 'root', '1234', 'users');
if (isset($_POST['search'])) {
$search = $_POST['search'];
$stmt = $db->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $search);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
echo "<table><tr><th>Name</th><th>id</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["username"] . "</td><td>" . $row["id"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
}
?>
【问题讨论】:
-
能否请您添加您的表结构?