2011-11-08 24 views
0

我有一個使用PHP的搜索功能,提供了按關鍵字或其他類別搜索的選項。爲了說明:PHP搜索關鍵字或其他類別

 Search by keyword: __________ 
     Search by the following (drop down menus): 
     1. Author 
     2. Category 
     3. Theme 
     4. Region 

其中許多錯誤我的是,如果用戶鍵入的東西到關鍵詞搜索字段,然後也從一個下拉菜單中選擇一個類別,搜索查詢是失敗(0結果)。無論用戶輸入什麼內容或從下拉菜單中選擇,我可以做些什麼來使其運行?

結果頁面的代碼如下。預先感謝您的幫助,您可以提供!:

連接:

<?php 

$dbcnx = @mysql_connect('localhost', 'root', 'password'); 

if (!$dbcnx) { 
    exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); 
} 

if ([email protected]_select_db('ijdb')) { 
    exit('<p>Unable to locate the joke ' . 'database at this time.</p>'); 
} 

$authors = @mysql_query('SELECT id, name FROM author'); 
if (!$authors) { 
    exit('<p>Unable to obtain author list from the database.</p>'); 
} 

$cats = @mysql_query('SELECT id, name FROM category'); 
if (!$cats) { 
    exit('<p>Unable to obtain category list from the database.</p>'); 
} 

$themes = @mysql_query('SELECT id, name FROM theme'); 
if (!$themes) { 
    exit('<p>Unable to obtain category list from the database.</p>'); 
} 

$geofoci = @mysql_query('SELECT id, name FROM geofocus'); 
if (!$geofoci) { 
    exit('<p>Unable to obtain category list from the database.</p>'); 
} 

?> 

的實際形式:

<form class="searchField" name="input" action="fundfetch_search.php" method="post"> 
    <ul> 
<li> 
    <label>Search by keyword:</label> 
    <input type="text" name="searchtext" class="styleSearchbox" placeholder="By keyword" value="<?php echo $_POST['searchtext']; ?>"> 
</li> 
<li> 
     <label>OR by the following: </label> 
     <label><select name="aid" size="1" class="styleDropdown"> 
     <option selected value="">Any Author</option> 
     <?php 
      while ($author = mysql_fetch_array($authors)) { 
       $aid = $author['id']; 
       $aname = htmlspecialchars($author['name']); 
       echo "<option value='$aid'>$aname</option>\n"; 
     } 
     ?> 
     </select></label>   
    </li> 
    <li> 
     <label><select name="cid" size="1" class="styleDropdown"> 
      <option selected value="">Any Category</option> 
     <?php 
     while ($cat = mysql_fetch_array($cats)) { 
      $cid = $cat['id']; 
      $cname = htmlspecialchars($cat['name']); 
      echo "<option value='$cid'>$cname</option>\n"; 
     } 
     ?> 
     </select></label> 
    </li> 
    <li> 
     <label><select name="tid" size="1" class="styleDropdown"> 
      <option selected value="">Any Theme</option> 
     <?php 
     while ($theme = mysql_fetch_array($themes)) { 
      $tid = $theme['id']; 
      $tname = htmlspecialchars($theme['name']); 
      echo "<option value='$tid'>$tname</option>\n"; 
     } 
     ?> 
     </select></label> 
    </li> 
    <li> 
     <label><select name="gfid" size="1" class="styleDropdown"> 
      <option selected value="">Any Region</option> 
     <?php 
     while ($geofocus = mysql_fetch_array($geofoci)) { 
      $gfid = $geofocus['id']; 
      $gfname = htmlspecialchars($geofocus['name']); 
      echo "<option value='$gfid'>$gfname</option>\n"; 
     } 
     ?> 
     </select></label> 
    </li> 
    <li style="visibility:hidden"><a href="../FUNDER.COM website/searchfilteroption">Closing</a></li> 
<li><input type="submit" value="Search" class="searchButton"></li> 
</ul> 
    </form> 

數據庫查詢:

 <?php 

$dbcnx = @mysql_connect('localhost', 'root', 'password'); 

if (!$dbcnx) { 
    exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); 
} 

if ([email protected]_select_db('ijdb')) { 
    exit('<p>Unable to locate the joke ' . 'database at this time.</p>'); 
    } 

// The basic SELECT statement 

$select = 'SELECT DISTINCT joke.id, joke.joketext, joke.jokedate, 
      author.id AS author_id, author.name AS author_name, 
      jokecategory.jokeid AS cat_jokeid, jokecategory.categoryid AS joke_catid, category.id AS cat_id, category.name as cat_name, 
      joketheme.jokeid AS theme_jokeid, joketheme.themeid AS joke_themeid, theme.id AS theme_id, theme.name AS theme_name, 
      jokegeofocus.jokeid AS geofocus_jokeid, jokegeofocus.geofocusid AS joke_geofocusid, geofocus.id AS geofocus_id, geofocus.name AS geofocus_name'; 
$from = ' FROM joke, author, jokecategory, category, joketheme, theme, jokegeofocus, geofocus'; 
$where = ' WHERE joke.authorid = author.id AND joke.id = jokecategory.jokeid AND jokecategory.categoryid = category.id AND joke.id = joketheme.jokeid AND joketheme.themeid = theme.id AND joke.id = jokegeofocus.jokeid AND jokegeofocus.geofocusid = geofocus.id'; 
$in = ' ORDER BY jokedate DESC'; 

$aid = $_POST['aid']; 
if ($aid != '') { // An author is selected 
    $where .= " AND authorid='$aid'"; 
} 

$cid = $_POST['cid']; 
if ($cid != '') { // A category is selected 
    $from .= ''; // usually written as ' ,tablename' 
    $where .= " AND joke.id=jokecategory.jokeid AND categoryid='$cid'"; 
} 

$tid = $_POST['tid']; 
if ($tid != '') { // A theme is selected 
    $from .= ''; 
    $where .= " AND joke.id=joketheme.jokeid AND themeid='$tid'"; 
} 

$gfid = $_POST['gfid']; 
if ($gfid != '') { // A region is selected 
    $from .= ''; 
    $where .= " AND joke.id=jokegeofocus.jokeid AND geofocusid='$gfid'"; 
} 

$searchtext = $_POST['searchtext']; 
if ($searchtext != '') { // Some search text was specified 
    $where .= " AND keywords LIKE '%$searchtext%'"; 
    } 
    ?> 

結果:

<?php 

    $jokes = @mysql_query($select . $from . $where . $in); 
    if (!$jokes) { 
     echo '</table>'; exit('<p>Error retrieving jokes from database!<br />'. 
     'Error: ' . mysql_error() . '</p>'); 
    } 

    $numrows = mysql_num_rows($jokes); 
    if ($numrows>0){ 
    while ($joke = mysql_fetch_array($jokes)) { 
     $id = $joke['id']; 
     $joketext = htmlspecialchars($joke['joketext']); 
     $jokedate = htmlspecialchars($joke['jokedate']); 
     $aname = htmlspecialchars($joke['author_name']); 
     $category = htmlspecialchars($joke['cat_name']); 
     $theme = htmlspecialchars($joke['theme_name']); 
     $geofocus = htmlspecialchars($joke['geofocus_name']); 
     $position = 200; 
     $post = substr($joketext, 0, $position); 
     echo "<li id=\"jump\"> 
       <article class=\"entry\"> 
        <header> 
         <h3 class=\"entry-title\"><a href=''>$aname</a></h3> 
        </header> 
        <div class=\"entry-content\"> 
         <p>$post...</p> 
        </div> 
        <div class =\"entry-attributes\"> 
         <p>> Category: $category</p> 
         <p> > Theme(s): $theme</p> 
         <p> > Region(s) of focus: $geofocus</p> 
         </div> 
        <footer class=\"entry-info\"> 
         <abbr class=\"published\">$jokedate</abbr> 
        </footer> 
       </article> 
      </li>"; 
    } 
} 
    else 
     echo "Sorry, no results were found. Please change your search parameters and try again!"; 
    ?>   
+1

[諷刺]請張貼更多代碼。[/ irony] ;-) –

+0

可以通過一個提交按鈕或者用JavaScript控制它(例如,如果某些內容已經輸入到搜索字段中,則禁用選擇菜單)。 – Quasdunk

回答

1

我甚至不知道從哪裏開始...

首先可言,從來沒有推你的用戶,請淨化你的數據庫輸入。

,代碼writen爲人們所理解,您在您的查詢下跌了很多重複的,我覺得是東陽難以閱讀查詢,例如AND joke.id = jokecategory.jokeid出現2次。

更多recomandations:

  • 使用inner joinon它使查詢更具可讀性
  • 不加引號封裝INT,它會破壞你的索引,使您的查詢慢。
  • ,如果你期待一個INT與$ VAL!=「」前is_numeric()來
  • 使用isset()函數檢查它,所以它不會拋出一個警告,如果在數組中的索引不存在
下面

見代碼:

$select = 'SELECT DISTINCT joke.id, joke.joketext, joke.jokedate, 
      author.id AS author_id, author.name AS author_name, 
      jokecategory.jokeid AS cat_jokeid, jokecategory.categoryid AS joke_catid, 
      category.id AS cat_id, category.name as cat_name, 
      joketheme.jokeid AS theme_jokeid, joketheme.themeid AS joke_themeid, theme.id 
      AS theme_id, theme.name AS theme_name, 
      jokegeofocus.jokeid AS geofocus_jokeid, jokegeofocus.geofocusid AS joke_geofocusid, 
      geofocus.id AS geofocus_id, geofocus.name AS geofocus_name'; 
$from = ' FROM joke 
      inner join author on (joke.authorid = author.id) 
      inner join jokecategory on (joke.id = jokecategory.jokeid) 
      inner join category on (jokecategory.categoryid = category.id) 
      inner join joketheme on (joke.id = joketheme.jokeid) 
      inner join theme on (joketheme.themeid = theme.id) 
      inner join jokegeofocus on (joke.id = jokegeofocus.jokeid) 
      inner join geofocus on (jokegeofocus.geofocusid = geofocus.id)'; 
$first_where = ' where '; 
$where = ''; 
$in = ' ORDER BY jokedate DESC'; 

if (is_numeric($_POST['aid'])) 
{ // An author is selected 
    $where.= $first_where.' authorid='.$_POST['aid']; 
    $first_where = ' and '; 
} 

if (is_numeric($_POST['cid'])) 
{ // A category is selected 
    $where.= $first_where.' categoryid='.$_POST['cid']; 
    $first_where = ' and '; 
} 

if (is_numeric($_POST['tid'])) 
{ // A theme is selected 
    $where.= $first_where.' themeid='.$_POST['tid']; 
    $first_where = ' and '; 
} 

if (is_numeric($_POST['gfid'])) 
{ // A region is selected 
    $where.= $first_where.' geofocusid='.$_POST['gfid']; 
    $first_where = ' and '; 
} 

if (isset($_POST['searchtext']) and $_POST['searchtext'] != '') 
{ // Some search text was specified 
    $where.= $first_where.' keywords LIKE "%'.(mysql_real_escape_string($_POST['searchtext'], $dbcnx)).'%"'; 
} 

if($where == '') 
{ // prevents returning the whole database, if form is empty 
    $where = ' limit 20'; 
} 

請測試我的代碼,並發表評論(它添加到你的答案,你沒有真正的意見聲譽),如果你仍然有問題(我沒有有時間來測試它)。

+0

嗨拉杜和感謝您的幫助。 我想你的代碼,並收到以下錯誤:解析錯誤:語法錯誤,意想不到的T_ENCAPSED_AND_WHITESPACE,期待T_STRING或T_VARIABLE或T_NUM_STRING在[文件目錄]行### 我也覺得從=,$被丟失的名稱我從中獲取數據的其他表格。 我還是很新的PHP,所以感謝您的安全和所有這些提示! – user1017566

+0

我糾正了代碼,一些排隊錯了...... –

+0

嗨拉杜,它似乎現在工作,我只是希望我更好地瞭解你所做的更改。例如,「不要將INT封裝在引號中,它會破壞您的索引並使查詢變慢。」我在哪裏做的? 再次感謝您的專業知識! – user1017566

0

我建議顯示混合結果,其中關鍵字存在於該類別中,然後可能在旁邊列出其他類別以及發現該關鍵字的計數。考慮像亞馬遜這樣的大型網站。如果我從分類列表中選擇「電影」並輸入「迪士尼」,我會看到與迪士尼相匹配的電影中最受歡迎的結果。然後在左邊,我有更多的選擇來縮小或擴大通過子/其他類別的搜索。