2015-07-12 78 views
0

我的网站有一个搜索表单,它工作得很好,但是我改变了整个登录系统,导致我的网站出现了一些错误。现在唯一不能解决的错误就是我网站中的搜索表单。我认为这很奇怪,因为表单不使用登录表单中的任何变量。当我进行测试以发现问题时,我发现当我的搜索脚本的部分进入while循环时,它会打断我。如果您有任何建议或帮助,将不胜感激。我将在下面提供我的代码。当我进入While循环时,PHP搜索无法正常工作

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()); 

我得到这个错误

警告:mysqli_query()预计至少2个参数,1 /services/webpages/d/i/digitalicon.ca/public/hub.php给出第42行

警告:mysqli_num_rows()预计参数1被mysqli_result, 空在/services/webpages/d/i/digitalicon.ca/public/hub.php给定上 线45

+0

错误是什么?你有没有调试过?\ –

回答

1

好的,所提供的更新信息看起来好像是通过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 

这包括我的实际查询数据库,在那里你以前只是逃避串初步修复。

+0

我刚把它改成你提供的例子,但它似乎没有改变任何东西。我注意到,虽然当我改变它时,我的短信编辑器中的颜色标识符在查询中的第一个“或”之后立即将mysql字体从粉红色变为黄色(就像它只是一个字符串而不是mysql字符串)。 – Brady

+0

运行代码时得到的错误是什么? – davids3

+0

看起来您的脚本实际上并未根据您发布的错误连接到数据库。你可以包含'include(“includes/connect.inc.php”)中包含的代码;'所以我们可以查看你的连接代码? (确保编辑连接的用户名/密码) – davids3