【问题标题】:PHP Search bar does not display dataPHP 搜索栏不显示数据
【发布时间】:2014-06-06 23:30:14
【问题描述】:

我在格式化我的代码时一定有问题,但找不到在哪里, 这是我放置搜索栏的形式:

<form>
        <?php include_once("searchbar.php"); ?>
    </form>

这是我的搜索条形码:

<form method= "post" action = "search.php">
        <input type="text" name="search_input" placeholder="Search Designer Sofas..." />
        <input type="submit" value="Search" />
    </form>

这是搜索结果页面的开始

    <!DOCTYPE html>
<?php
   //retrieves the specified element from the URL
   $search_input = $_POST["search_input"];

   include_once("connection_to_db.php");

   //isset function checks if a variable is set
   if(!isset($search_input))
   {
        print("Nothing to search !");
        return;
   }

    //take the books with the specific name
    $query_products = "SELECT * FROM XXX WHERE name LIKE '%".$search_input."%'";

    $query_product_result = mysql_query($query_products)
     or die(mysql_error());

    $num_rows_products = mysql_num_rows($query_product_result);

?>

<html lang="en">

<head>

现在发生的情况是,当我单击搜索按钮时,搜索查询的 url 中的地址发生了变化,但页面没有,这是否意味着它正在发送给它但没有收到任何东西?

【问题讨论】:

  • 您应该使用 mysqli 或 PDO 以及准备好的语句,否则 Bobby Tables 会破坏您的数据库。

标签: php html database search


【解决方案1】:

enter = 签署行动

<form method="post" action="search.php">
    <input type="text" name="search_input" placeholder="Search Designer Sofas..." />
    <input type="submit" value="Search" />
</form>

并尝试从中删除表单标签

<?php include_once("searchbar.php"); ?>

【讨论】:

  • 我真的希望就是这样,但它只是做同样的事情。只是 url 更改为 /index.php?search_input=asd 页面没有更改
  • 您是在表单页面尝试ctrl+f5还是ctrl+r然后再次提交表单?
  • 是的,再次上传到服务器以防万一,还是没有
  • 我检查了你的答案,仍然只是更改了网址
  • 请粘贴您的浏览器生成的源代码...然后我们会找到问题
【解决方案2】:

看起来您已经嵌套了&lt;form&gt; 元素。这就是您的表单呈现的方式:

<form>
<form method= "post" action = "search.php">
        <input type="text" name="search_input" placeholder="Search Designer Sofas..." />
        <input type="submit" value="Search" />
</form>
</form>

第二种形式(带有动作的形式)将被忽略。删除包含searchbar.php&lt;form&gt; 包装器。

您也很容易受到 SQL 注入攻击。考虑使用mysqli prepared statements

【讨论】:

  • 这似乎起作用了,搜索但结果被隐藏了
猜你喜欢
  • 2021-01-25
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
  • 2019-02-05
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
  • 2011-02-08
相关资源
最近更新 更多