2015-11-06 166 views
1

我試圖在我的網站上做一個搜索欄,用來查詢mysql數據庫並以一種時尚的方式返回結果。搜索將用於查找名稱。例如,用戶可以輸入「John Doe」,並且關於John Doe的任何信息將被返回。不幸的是,每當我點擊提交按鈕,我的網站轉換到'search.php'頁面是完全空白的。我測試了與數據庫的連接並知道它正在工作。我的代碼有什麼問題,以至於什麼都沒有顯示出來?它甚至不會將用戶恢復到index.html。查詢並顯示mysql搜索結果

我有我的index.html一個搜索欄,如下所示:

 <form id="navsearch" class="form-group" role="search" action="search.php" method="post"> 
      <div class="input-group" display="inline"> 
       <input name="search" type="text" value="Search" class="form-control" placeholder="Search"> 
       <span class="input-group-btn"> 
        <button class="btn btn-default btn" name="submit" class="btn btn-lg" type="submit"> 
         <span class="glyphicon glyphicon-search" name="submit"></span> 
        </button>  
      </div> 
     </form> 

這裏是 '的search.php' 的PHP:

<?php 

//make connection to database 

    $connection = mysqli_connect('xxx.xxxx', 'root', 'hello', 'awstutorial', 3306); 

$search_word = ''; 
if (isset($_POST['submit'])) { 
    if (!empty($_POST['search'])) { 
    $search_word = $_POST['search']; 
    $search = mysqli_real_escape_string($connection,  $_POST['search']); 

    $query = "SELECT * FROM data WHERE name LIKE '%$search%' "; 
    $search_query = mysqli_query($connection, $query); 

    // START RESULTS 

    if ($result = mysqli_query($connection, $query)) { 
     $to_display = "<div>"; 


     while ($row = mysqli_fetch_array($result)) { 
      if ((substr($row['file'], -3) == 'jpg') || (substr($row['file'], -3) == 'JPG') || (substr($row['file'], -3) == 'PNG') || (substr($row['file'], -3) == 'png') || (substr($row['file'], -3) == 'GIF') || (substr($row['file'], -3) == 'gif')) 
       $img_url = "uploads/" . $row['file']; 
      else { 
       if ((strtolower(substr($row['file'], -3) == 'png')) || (strtolower(substr($row['file'], -3) == 'doc')) || (strtolower(substr($row['file'], -3) == 'docx'))) 
        $img_url = "uploads/pdf_logo.png"; 
       else 
        $img_url = "uploads/no_doc.png"; 
      } 
      $to_display .= "<a href='story.php?id=".$row['id']."' class='col-md-3 asdivs portfolio-box fancybox fancybox.iframe'><img alt='' class='img-responsive' src='" . $img_url . "'><div class='portfolio-box-caption'> 
          <div class='portfolio-box-caption-content'> 
           <div class='project-category text-faded'> 
            Read 
           </div> 
           <div class='project-name'>" 
         . $row['name'] . "'s Story 
           </div> 
          </div> 
         </div>" . $row['name'] . " - " . $row['email'] . "</a>"; 

     } 
    } 

    $to_display .= "</div><div style='clear:both;'></div>"; 
} else { 
    header("Location: index.html"); 
    } 
} 

?> 
+1

嘗試在你的PHP安裝[啓動錯誤報告(http://stackoverflow.com/a/21429652/747654),並再次運行該腳本 – wil93

+2

您正在使用類型的按鈕,'submit'這將通過非常自然的方式提交表單 - 它被設置爲'search.php' - 你期望發生什麼?如果你想爲你的用戶提供一個異步的,單一的頁面,沒有重載的經驗,你將需要使用Ajax來運行查詢,而無需重新加載頁面 – RamRaider

+0

刪除'if(isset($ _ POST ['submit'])){...} ' – c4pricorn

回答

0

我從知道什麼是很遠在提交表單時我的意思是fashionable way - 我能想到的最好的一點是,它指的是提交搜索而不重新加載頁面的ajax請求,然後以某種方式在同一頁面上向用戶顯示結果?由於沒有ajax的證據,很難說。

正如我在評論中提到的,您的查詢引用$search_query,但通過記錄集的迭代引用$result - 這些需要是相同的變量,這可能是爲什麼你面臨一個空白屏幕 - 事實和變量$to_display從未回顯給瀏覽器。

如果有疑問,請使用error_reporting來突出顯示事情可能出錯的地方。

<?php 
    /* Sessions? */ 
    session_start(); 

    error_reporting(E_ALL); /* Change this for live site! */ 

    if($_SERVER['REQUEST_METHOD']=='POST'){ 

     $conn = mysqli_connect('xxx.xxxx', 'root', 'hello', 'awstutorial', 3306); 
     $exts_img=array('jpg','png','gif','jpeg'); 
     $exts_doc=array('pdf','doc','docx'); 
     $html=array(); /* use this to construct output. More efficient that string concatenation. */ 

     if (isset($_POST['submit'] , $_POST['search']) && !empty($_POST['submit']) && !empty($_POST['search'])) { 

      /* It is better to use prepared statements or pdo */ 
      $search = mysqli_real_escape_string($conn,$_POST['search']); 
      $sql = "SELECT * FROM `data` WHERE `name` LIKE '%".$search."%';"; 
      $results = mysqli_query($conn, $sql); 


      if($results && mysqli_num_rows($results) > 0){ 
       $html[]="<div>"; 

       while($row = mysqli_fetch_object($results)) { 

        $id  = $row->id; 
        $file = $row->file; 
        $email = $row->email; 
        $name = $row->name; 
        $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); 



        if(in_array($ext, $exts_img)) { 
         $img_url = "uploads/" . $file; 
        } else { 
         if(in_array($ext, $exts_doc)) $img_url='uploads/pdf_logo.png'; 
         else $img_url='uploads/no_doc.png'; 
        } 


        $html[]=" 
         <a href='story.php?id=".$id."' class='col-md-3 asdivs portfolio-box fancybox fancybox.iframe'> 
          <img alt='' class='img-responsive' src='" . $img_url . "' /> 
          <div class='portfolio-box-caption'> 
           <div class='portfolio-box-caption-content'> 
            <div class='project-category text-faded'>Read</div> 
            <div class='project-name'>{$name}'s story</div> 
           </div> 
          </div> 
          {$name} - {$email} 
         </a>"; 
       } 
       $html[]="</div><div style='clear:both'></div>"; 
      } 
      /* No results */ 
      $html[]='<h1>Sorry, no results...</h1>'; 

     } else { 
      header("location: index.html"); 
     } 

     /* free resources */ 
     mysqli_free_result($results); 
     mysqli_close($conn); 

     /* output stuff to the browser */ 
     echo implode(PHP_EOL, $html); 
    } 
?>