2015-10-15 61 views
2

我在幾個星期的空閒時間裏學習了PHP,這是我必須要求幫助的第一個問題。我在互聯網上搜索了所有內容,但是當涉及到具有兩個輸入字段的PHP mysql搜索引擎時,我沒有發現任何東西。使用兩個輸入字段的PHP搜索引擎

我有一個搜索引擎,它有兩個輸入字段,一個字段用於輸入所尋找的業務種類。第二場是進入哪一個是試圖找到在企業的位置

可能有人真棒,高度智能化的,請告訴我,我如何獲得:

第一個輸入字段通過我business欄搜索在我的MySql表& 第二個輸入字段中搜索我MySql表中的我的location列。

我使用的搜索引擎方法是FULLTEXT

這裏是表單代碼: (我知道這是真的馬虎,而且很可能不安全,但我計劃在固定的後來。)

<form id="tfnewsearch" method="get" action="search.php"> 
    <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
     <tr> 
     <td> 
      <table width="476" border="0" cellspacing="0" cellpadding="0"> 
       <tr valign="middle"> 
        <td width="476" class=""> 
        <div id="tfheader2"> 
         <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
          <tr> 
           <td width="91%"> 
           <table width="472" border="0" cellspacing="0" cellpadding="0"> 
            <tr> 
             <td width="4">&nbsp;</td> 
             <td width="139"><input style="width:210px" type="text" id="tfq" class="tftextinput2" name="business" size="20" maxlength="40" value="Business e.g. Insurance" onClick="if(this.value == 'Business e.g. Insurance') this.value='';" /></td> 
             <td width="5">&nbsp;</td> 
             <td width="69"><input style="width:210px" type="text" id="tfq2" class="tftextinput2" name="location" size="20" maxlength="40" value="Location e.g. Hamilton" onClick="if(this.value == 'Location e.g. Hamilton') this.value='';" /></td> 
             <td width="4">&nbsp;</td> 
             <td> 
              <input style="width:40px" type="image" src="site_images/q.png" alt="Search" class="tfbutton2" name="submit" value="&gt;" /> 
             </td> 
             <td width="10">&nbsp;</td> 
            </tr> 
           </table> 
           </td> 
          </tr> 
         </table> 
         <div class="tfclear"></div> 
        </div> 
        </td> 
       </tr> 
      </table> 
     </td> 
     </tr> 
    </table> 
</form> 

這裏是PHP代碼片段:

$button = @$_GET['submit']; 
$search1 = @$_GET['business']; 
$search2 = @$_GET['location']; 

$strSQL = "SELECT * FROM users WHERE region = $search1 AND city = $search2"; 

$query = mysqli_query($con, $strSQL); 
while ($result = mysqli_fetch_array($query)); 

if ($result = mysqli_query($con, $strSQL)) 

    $foundnum = mysqli_num_rows($result); 

if ($foundnum == 0) 

如果您需要更多信息,請讓我知道。

+0

任何錯誤消息? –

+1

您的標記(html)有許多不匹配的標記。我注意到一些未關閉的開放標籤(反之亦然)。請從這裏開始,格式化標記,以便它可讀(我試着爲你準備,但不匹配的標籤使它很難) – Christian

+1

你正在以非常錯誤的方式使用mysqli –

回答

0

將變量綁定到查詢時,應該使用勾號(')。

$strSQL = "SELECT * FROM users WHERE region = '$search1' AND city = '$search2'"; 

而爲了得到結果,你可以使用mysqli_fetch_array()來獲取結果。這裏有一個例子:

$query = mysqli_query($con, $strSQL); /* EXECUTE FIRST THE QUERY */ 
while($result = mysqli_fetch_array($query)){ 
    echo $result["column1"]." ".$result["column2"]."<br>"; /* REPLACE NECESSARY COLUMN NAMES */ 
} 

你也應該考慮使用*_real_escape_string()防止一些SQL injections你將它們綁定到你的查詢之前。

$search1 = mysqli_real_escape_string($con, $_GET['business']); 

而不是value標籤爲您Business e.g. Insurance,你可以只使用placeholder所以你不會有包括onClick=""標籤。

<input type="text" id="tfq" class="tftextinput2" name="business" size="20" maxlength="40" style="width:210px" placeholder="Business e.g. Insurance"/> 

你還應該看看prepared statement,而你在這裏。

+0

非常感謝大家,尤其是你在這個時候Logan,大大的幫助謝謝。現在我只需要整理我的分頁,它不再工作。我試圖自己修復它,但如果我絕對不能弄明白的話,會尋求幫助。謝謝。和平! – Hoani