2012-02-13 169 views
0

我知道的HTML和其他很少。SQL搜索引擎

我想爲我的網站放在一起一個簡單的搜索引擎。我有3個表,我想加入,然後有數據可供搜索。

MESSAGES (ID_TOPIC, SUBJECT) 
TAGS (ID_TAG, TAG) 
TAGS_LOG (ID_TAG, ID_TOPIC) 

我試圖創建具有三個表的陣列(消息,標籤,和TAGS_LOG),所以我可以創建與MESSAGES.SUBJECT搜索標籤和輸出該數據作爲超鏈接,其中超鏈接的搜索引擎是www.website.com/ID_TOPIC

我看了所有,我已經接近,但沒有錢。

到目前爲止,我有;

CREATE TABLE new_table 
    AS (SELECT tags.id_tag, messages.subject, tags.tag, tags_log.id_topic 
     FROM messages, tags, tags_log); 

它用於將信息組合成單個表進行搜索。接下來我創建了search.php;

<h2>Search</h2> 
<form name="search" method="post" action="<?=$PHP_SELF?>"> 
Seach for: <input type="text" name="find" /> in 
<Select NAME="field"> 
<Option VALUE="tag">tag</option> 


</Select> 
<input type="hidden" name="searching" value="yes" /> 
<input type="submit" name="search" value="Search" /> 
</form> 

<? 
$field = $_POST['field'] ; 
$find = $_POST['find'] ; 
$searching = $_POST['searching'] ; 

//This is only displayed if they have submitted the form 
if ($searching =="yes") 
{ 
echo "<h2>Results</h2><p>"; 

//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// Otherwise we connect to our Database 
mysql_connect("host", "user", "pass") or die(mysql_error()); 
mysql_select_db("database") or die(mysql_error()); 

// We preform a bit of filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search for our search term, in the field the user specified 
$data = mysql_query("SELECT * FROM new_table WHERE upper($field) LIKE'%$find%'"); 

//And we display the results 
while($result = mysql_fetch_array($data)) 
{ 
echo " "; 
echo "Topic ID: {$result['id_topic']}"; 
echo "<br>"; 
Print "<a href=/index.php?topic={$result['id_topic']} target=new>{$result['subject']} </a>"; 
echo "<br>"; 
echo "<br>"; 
} 

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 

//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find; 
} 
?> 

但是我在主題方面遇到問題,我從Subject和RE:Subject獲得結果。我不知道如何清除「RE:Subject」,所以沒有重複的條目。

+0

相當寬泛的問題。請嘗試包括你所做的事情和你被卡住的地方, – Randy 2012-02-13 22:11:55

+1

問題到底是什麼? – 2012-02-13 22:12:22

+1

你說你有「看遍了」,但你有沒有去過「sql join tutorial」?學習一些SQL的基礎知識,你將能夠回答你自己的問題。 – Tony 2012-02-13 22:17:53

回答

0

嘗試:SELECT TAGS.TAG, MESSAGES.SUBJECT, MESSAGES.ID_TOPIC FROM MESSAGES INNER JOIN TAGS_LOG ON MESSAGES.ID_TOPIC = TAGS_LOG.ID_TOPIC INNER JOIN TAGS ON TAGS_LOG.ID_TAG = TAGS.ID_TAG

但由於在評論中提到的,這是一個SQL 101的問題。然而,這並不像建議的那麼微不足道,因爲在那裏有一個額外的內部連接,這必然會使任何新手都不確定自己。

+0

我省略了MESSAGSE.ID_TOPIC - 我已將它添加進去。 – Kaganar 2012-02-13 22:30:39

+0

您正在將註釋添加到線程的錯誤部分 - 在問題之後將其添加到上面,而不是在此答案之後。 – Kaganar 2012-02-13 22:36:32

+0

我知道如何使用JOIN – 2012-02-14 01:36:31