当我进入 While 循环时 PHP 搜索不工作

标签 php mysql forms loops search

我的网站有一个搜索表单,运行得很好,但后来我更改了整个登录系统,这导致我的网站出现一些错误。现在我似乎无法修复的唯一错误是我网站内的搜索表单。我认为这很奇怪,因为该表单不使用登录表单中的任何变量。当我测试找出问题时,我发现当我的搜索脚本部分进入 while 循环时,它就会中断。如果您有任何建议或帮助,我们将不胜感激。我将在下面提供我的代码。

hub.php

 <?php

  // this file connects to the database
  include("includes/connect.inc.php");

  session_start();

  if (isset($_SESSION['id'])) {
    $uid        = $_SESSION['id'];
  } 

//If the search input was submitted then run
if(isset($_POST['search'])){
    // turn that the user searched into a varible
    $searchQ = $_POST['search'];
    // delete any symbols for security
    $searchQ = preg_replace("#[^0-9a-z]#i", "", $searchQ);
    // Define varibles before the loop
    $searchArray = array();
    $searchIndex = 0;

    // Search through these columns inside the main database
    $searchQuery = mysqli_query("SELECT * FROM database WHERE 
        title   LIKE '%" . mysqli_escape_string( $searchQ ) . "%' 
    ");

    // count the number of results
    $searchCount = mysqli_num_rows($searchQuery);
    if($searchCount != 0){
        while($row = mysqli_fetch_array($searchQuery)){
            $titleSearch     = $row['title'];
            $dateSearch      = $row['date'];

            // buile the array which will hold all the results
            $searchArray[$searchIndex] = array($titleSearch, $dateSearch);
            $searchIndex++;
        }
    }
}
// End of search php



<form id="search" action="hub.php" method="POST">
    <div id="searchIcon"></div>
    <input type="search" name="search" placeholder="Search">
</form>

这是连接文件|它用于许多其他工作正常的表单和循环。

<?php

    $host = "MYHOSTINGDOMAIN";
    $username = "MYUSERNAME";
    $password = "MYPASSWORD";
    $db = $username;

    $connect = mysqli_connect($host, $username, $password, $db) or die(mysqli_connect_error());

我收到此错误

Warning: mysqli_query() expects at least 2 parameters, 1 given in /services/webpages/d/i/digitalicon.ca/public/hub.php on line 42

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /services/webpages/d/i/digitalicon.ca/public/hub.php on line 45

最佳答案

好的,根据提供的更新信息,您似乎是通过 mysqli_* 函数进行连接,然后在该脚本中使用 mysql_* 函数(切换到 mysqli 可能是登录更改的一部分)。

因此您需要更新脚本以使其变为:

<?php

// this file connects to the database
include("includes/connect.inc.php");

session_start();

if (isset($_SESSION['id'])) {
    $uid        = $_SESSION['id'];
} 

// //If the search input was submitted then run
if(isset($_POST['search'])){
    // turn that the user searched into a varible
    $searchQ = $_POST['search'];
    // delete any symbols for security
    $searchQ = preg_replace("#[^0-9a-z]#i", "", $searchQ);
    // Define varibles before the loop
    $searchArray = array();
    $searchIndex = 0;

    // Search through these columns inside the main database
    $searchQuery = mysqli_query("SELECT * FROM database WHERE 
        title   LIKE '%" . mysqli_escape_string( $searchQ ) . "%' or 
        date    LIKE '%" . mysqli_escape_string( $searchQ ) . "%' 
    ");

    // count the number of results
    $searchCount = mysqli_num_rows($searchQuery);
    if($searchCount != 0){
        while($row = mysqli_fetch_array($searchQuery)){
            $titleSearch     = $row['title'];
            $dateSearch      = $row['date'];

            // buile the array which will hold all the results
            $searchArray[$searchIndex] = array($titleSearch, $dateSearch);
            $searchIndex++;
        }
    }
}
// End of search php

这包括我对实际查询数据库的初步修复,您之前只是在其中转义字符串。

关于当我进入 While 循环时 PHP 搜索不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31363851/

相关文章:

html - XHTML 严格验证

PHP 日期将 5 年添加到当前日期

javascript - Ajax 调用不起作用?

php - CKeditor 和 TinyMCE 在发布的内容上输出 HTML 标签

Python 程序在使用 py2exe 转换为 exe 后不处理错误

javascript - 使用重力形式根据条件添加输入字段

php - symfony - 下载文件

PHP 在远程服务器上读取/写入文件

php - 如何防止刷新页面时更新mysql字段+1

forms - 使用流体网格系统在 Bootstrap 上对齐输入