2012-12-04 73 views
0

好吧,所以我想爲我的網站編寫一個小小的PHP搜索腳本,以便用戶可以簡單地從藝術家姓名,歌曲名稱或城市進行搜索。 我的數據庫中的表格有'城市','藝術家'和'城市'。網站的PHP搜索腳本

這裏是我的形式:

<div id="search"> 
<form name="search" method="post" action="../searchDb.php"> 
<input type="text" name="find" placeholder="What are we searching for ?"/> in 
<Select NAME="field"> 
<Option VALUE="artist">Artist</option> 
<Option VALUE="song">Song</option> 
<Option VALUE="city">City</option> 
</Select> 
<input type="hidden" name="searching" value="yes" /> 
<input type="submit" name="search" value="Search" /> 
</form> 
</div> 

正如你可以看到,有三個選項值(一個在我的表中的每一列)。 這是我的PHP代碼:

<?php 
$searching = "searching"; 
$find = "find"; 
$field = "field"; 
//this is to make sure the user entered content 
if ($searching =="yes") 
{ 
    echo "<p><h2>Results</h2></p>"; 

    //if user did not enter anything in the search box, give error 
    if ($find == "") 
    { 
     echo "<p>You forgot to enter a search term</p>"; 
    } 

    include 'connect.php'; 

    // strip whitespace, non case sensitive 
    $find = strtoupper($find); 
    $find = strip_tags($find); 
    $find = trim ($find); 

    //perform search in specified field 
    $data = mysql_query("SELECT * FROM artists_table WHERE upper($field) LIKE'%$find%'"); 

    //show results 
    while($result = mysql_fetch_array($data)) 
    { 
     echo $result['artist'];  
     echo " "; 
     echo $result['song'];  
     echo "<br>";  
     echo $result['city'];  
     echo "<br>";  
     echo "<br>"; 
    } 

    //counts results. ifnone. error  
    $anymatches=mysql_num_rows($data);  
    if ($anymatches == 0)  
    {  
     echo "Sorry, but we can not find an entry to match your query<br><br>";  
    }  

    //show user what he searched. 
    echo "<b>Searched For:</b> " .$find;  
}  
?>  

我connect.php(即包括)完美地工作(我有另一頁上工作的同一個文件,沒有problems..So其安全地說那不是問題) 。

當我做一個測試並運行一個搜索,它加載我的searchDb.php但沒有顯示。只是一個白頁...

任何幫助將不勝感激。我失去了爲什麼或什麼不工作... 謝謝你們!

+0

不要忘記正確š將查詢中的數據進行匿名化處理,今天就是''find''明天是'$ _GET' ... – soulseekah

+0

順便說一句,您不必大寫字符串搜索,因爲LIKE不區分大小寫,只要您不處理用二進制字符串。 – RonaldBarzell

+0

你的意思是明天它的「找到」明天它的$ _GET? – user1853848

回答

1

如果這是你的代碼,那麼你硬編碼$searching = "searching",但在你的if你檢查是否$searching =="yes",所以沒有代碼會顯示。

<?php 
$searching = "searching"; 
... 
... 
//this is to make sure the user entered content 
if ($searching =="yes") 
{ 
... 
} 

編輯 -

我的猜測是,你想做一些喜歡 -

$searching = mysql_real_escape_string($_POST['searching']); // sanitized just to be consistant. 
$find = mysql_real_escape_string($_POST['find']); 
$field = mysql_real_escape_string($_POST['field']); 

注意 - 你不應該mysql_*功能來編寫新的代碼。你應該學會要麼mysqli_PDO - http://php.net/manual/en/mysqlinfo.api.choosing.php

這裏有2種方式,以避免收到「通知:未定義變量」

檢查submit按鈕被按下

if (isset($_POST['search'])) { 
$searching = mysql_real_escape_string($_POST['searching']); // sanitized just to be consistant. 
$find = mysql_real_escape_string($_POST['find']); 
$field = mysql_real_escape_string($_POST['field']); 
} 

或檢查每個字段是集,並將其設置爲值,如果沒有設置爲無/空

if (isset($_POST['search'])) { // checks to see if the form submit button was pushed 
$searching = isset($_POST['search']) ? mysql_real_escape_string($_POST['searching']) : 'no'; // sanitized just to be consistant. 
$find = isset($_POST['find']) ? mysql_real_escape_string($_POST['find']) : ''; 
$field = isset($_POST['field']) ? mysql_real_escape_string($_POST['field']) : ''; 
} 
+0

我嘗試刪除前3行($ searching =「searching」,$ find = ...... ),當我重新運行代碼時,請注意:未定義變量:在C中搜索....... – user1853848

+0

請參閱我的編輯。你必須設置變量'$ searching',否則你會得到'Notice:Undefined variable:searching ...',但是你會想要將它設置爲你的表單輸入的'post'的值 - ' – Sean

+0

非常感謝Sean。 我使用了您編輯的代碼($ searching = mysql_real_escape_string($ _ POST ['searching']);而我仍然收到通知:未定義..... – user1853848