【问题标题】:Im trying to use prepared statements with a search bar php我试图在搜索栏 php 中使用准备好的语句
【发布时间】: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";
    }
}
?>

【问题讨论】:

  • 能否请您添加您的表结构?

标签: php html search


【解决方案1】:

您的按钮和输入都具有名称“搜索”,因此,您的 $_POST['search'] 不包含相关值。更具体地说,它将始终是空字符串 "",因为它将采用具有此名称的最后一个字段的值(例如按钮)。

尝试更改输入的名称或提交按钮的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 2013-08-31
    相关资源
    最近更新 更多