2015-07-12 28 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