2012-06-05 78 views
-3

所以我建立一個搜索網站,那裏是一個PHP/MySQL的搜索和一個javascript「顯示此數目每頁結果」 HTML選擇它與數每頁提交的用戶然後刷新搜索頁面。不幸的是,當我加載頁面時,我得到了三個錯誤。 if (isset($_POST['select'])){ $total_pages == $_POST['select'];}只設置爲$total_pages = 12它工作正常嗎?問題與PHP選擇/搜索

  • 注意:您的SQL語法錯誤;檢查 對應於你的MySQL服務器版本正確的語法在/Users/Me/Sites/mySite/search.php上線使用 「」附近,在1號線手冊36
  • 警告:mysql_num_rows()預計參數1是資源,在布爾給出/Users/Me/Sites/mySite/search.php上線37
  • 警告:行中/Users/Me/Sites/mySite/search.php被零除104

下面是代碼:

<?php 

    $q = mysql_real_escape_string(ucfirst(trim($_REQUEST['searchquery']))); 

     if (isset($q)){ 
     if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; 
     if (isset($_POST['select'])){ $total_pages == $_POST['select'];} //Here edit the amount per page 
     $record_start = ($page * $total_pages) - $total_pages; 
      REQUIRE('config.php'); 

      $result = mysql_query("SELECT * FROM companies WHERE company_name LIKE '%$q%' OR company_description LIKE '%$q%' OR cat1 LIKE '%$q' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' OR company_phone LIKE '%$q' ORDER by company_name LIMIT $record_start,$total_pages") or trigger_error(mysql_error()); 
      $rows = mysql_num_rows($result); 

      $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM companies"),0); 
      echo " 
      <h4>Search for &quot;$q&quot;</h4> 
      <div class='right'> 
       <div class='textad1'> 
        <center> 

         <form name='form1' method='post'> 
          Results per Page: 
          <select name='select' onChange='document.form1.submit()'> 
           <option value='10'";if($_REQUEST['select'] == 12) {echo "selected='selected'";} echo">12</option> 
           <option value='15'";if($_REQUEST['select'] == 18) {echo "selected='selected'";} echo">18</option> 
           <option value='25'";if($_REQUEST['select'] == 32) {echo "selected='selected'";} echo">32</option> 
          </select> 
         </form> 

        </center> 
       </div> 
       <div class='divider'></div> 
      </div> 
      <div class='left'> 

      <ul>"; 
     while($row = mysql_fetch_array($result)) 

如果甲腎上腺素編輯更多的代碼,我會很快得到你的。任何幫助將不勝感激!

+0

你連接到數據庫執行查詢之前?如果沒有,那麼你的問題就是mysql錯誤。 – Vulcan

+0

所以你寫了100行代碼,甚至沒有嘗試在這個過程中運行它?你不能在同一時間寫的100行沒有錯誤 - 你執行更小的步驟,並檢查代碼運行正常後,每條線增加 – zerkms

+0

@Vulcan我有一個數據庫連接在這之前我只是沒有貼,還有的地方我定義$ q ...如果您認爲它會有所幫助,我可以發佈。 –

回答

1

我覺得你的代碼應該是這樣的,

require('config.php'); 
$q = mysql_real_escape_string(ucfirst(trim($_REQUEST['searchquery']))); 

    if (isset($q)){ 
    if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; 
    if (isset($_POST['select'])){ $total_pages = $_POST['select'];} //Here edit the amount per page 
    $record_start = ($page * $total_pages) - $total_pages; 

     $result = mysql_query("SELECT * FROM companies WHERE company_name LIKE '%$q%' OR company_description LIKE '%$q%' OR cat1 LIKE '%$q' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' OR company_phone LIKE '%$q' ORDER by company_name LIMIT $record_start,$total_pages") or trigger_error(mysql_error()); 
     $rows = mysql_num_rows($result); 

     $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM companies"),0); 

//code continuation