2011-08-25 71 views
0

我想在查詢中連接2個表以顯示結果。
我在第一個表中有lastname,firstname等。
在第二個表我有trainingdate ...mysql SELECT(需要連接到2個表格並顯示搜索結果)

我也有第二個表的ID參考鏈接姓氏和名字,以培訓和日期。
因此,只要我在數據庫中創建培訓記錄,就會爲同一個人使用相同的ID。
我希望能夠連接我的查詢中的兩個表,並顯示結果,如果我搜索lastname
它還會列出他們已完成的所有培訓,如果我在搜索中點擊該名稱。
我很新的MySQL數據庫。
我正在使用phpmyadmin的MySQL和運行本地主機數據庫的測試目的...
我目前可以搜索一個表,並提出結果,但我無法弄清楚它們的聯合,並將名稱列表作爲鏈接顯示在那裏培訓。並讓它保持在打印輸出頂部的名稱...這是我使用的代碼,我從教程中獲得。

<form> 
    <div align="right"> 
    <input type="button" value="Print Results" onClick="myprint()"> 
    </div> 
</form> 
<?php 

// TAKE THE INFORMATION FROM FORM. 
$search = $_GET['search']; 

// IF THERE IS NOT A KEYWORD GIVE AN ERROR 
if (!$search) 
echo "You didn't enter a keyword"; 
else 
{ 
    echo "<td>You searched for: <strong>$search </strong></td>"; 
    mysql_connect('localhost','loginid','password'); 
    mysql_select_db('trainingrecords'); 
    [email protected]$_GET['id']; 

    //QUERY IS THE CODE WHICH WILL MAKE THE SEARCH IN YOUR DATABASE. 
    //I WROTE AN EXPLANATION ABOUT IT AFTER THE CODE. 
    $query="SELECT * FROM username 
      WHERE MATCH(lastname, firstname, location, created) 
      AGAINST('%$search%' IN BOOLEAN MODE)"; 
    $result1 = MySQL_query($query); 
    if(!$result1) { 
    echo MySQL_error()."<br>$query<br>"; 
    } 
    if (MySQL_num_rows($result1) > 0) { 
    echo "<table width='750' align='center' border='1' 
      cellspacing='2' cellpadding='2'>"; 
    while($result2 = MySQL_fetch_array($result1)) { 

     //A short description from category. 
     $description = $result2['location']; 
     $searchPosition = strpos($description, $search); 
     $shortDescription = substr($description, $searchPosition, 150); 

     echo "<td><strong>{$result2['lastname']} 
      {$result2['firstname']}</td></strong><td> 
      $shortDescription</td><td>{$result2['created']}</td><tr/>"; 
    } 
    echo "</table>"; 
    } else { 
    echo "No Results were found in this category.<br>"; 
    } 
    echo "<br>"; 
} 
?> 

回答

1

1 - 你有一個SQL注入孔
確保使用mysql_real_escape_string
逃避所有的變量,並利用正確報價查詢字符串裏面_(否則mysql_real_escape_string將無法正常工作)_

$search = mysql_real_escape_string($GET['search']); 
.... 
$query="SELECT * FROM username 
     WHERE MATCH(lastname, firstname, location, created) 
     AGAINST('%".$search."%' IN BOOLEAN MODE)"; 
//   ^   ^quotes were already there, good! 

2 - 如果你想從多個表的結果,你就必須做一個連接

$search = "SELECT u.*, t.* FROM username u 
    INNER JOIN training t ON (u.id = t.user_id) 
    WHERE MATCH(lastname, firstname, location, created) 
     AGAINST('%".$search."%' IN BOOLEAN MODE)"; 

通常情況下,您不想選擇所有字段,如果您明確聲明要顯示給用戶的字段,查詢將運行得更快。

這裏的PHP和MySQL的一個體面的教程:http://www.tizag.com/mysqlTutorial/
而這裏的,如果你有SQL注入孔時會發生什麼:How does the SQL injection from the "Bobby Tables" XKCD comic work?

+0

感謝約翰這個偉大的工程!現在我唯一的問題是我的字符串搜索。當我搜索姓氏,並且如果有兩個姓氏相同的人帶來了他們兩個,當我一起搜索姓氏姓氏。它似乎無視它,只是把兩者都拉上來。我怎樣才能讓它在我的搜索中僅匹配名字和姓氏。而不僅僅是尋找並給我所有結果? – yeahwhatever

+0

我也無法按姓氏或我選擇的方式排序搜索。有沒有辦法讓我的桌子上的每個領域的標題,如果你點擊它們,它們通過該領域命令他們?並且默認是姓氏? – yeahwhatever

1

我想你想是這樣的:

SELECT * FROM username, training 
    where username.lastname = $lastname and username.firstname = $firstname 
     and username.id = training.id; 

這需要你在$lastname$firstname值作爲獨立變量傳遞。

1

它不是工會的加入你想

Select a.id,a.firstname,a.lastname,a.location,a.date,b.trainingdate 
from username a 
inner join 
training b on a.id=b.id 

添加您的地方在年底條款

0
-- something basic like this? 

SELECT u.id, u.name, t.link 
FROM users u 
LEFT JOIN trainings t ON u.id = t.user_id 
WHERE u.id = 5 
0

也許你想是這樣的:

SELECT username.* FROM username 
JOIN training ON (training.id = username.id) 
WHERE MATCH(lastname, firstname, location, created)  
     AGAINST('%$search%' IN BOOLEAN MODE) 
1

你需要的是a JOIN

$query="SELECT * FROM username u LEFT JOIN training t on (t.userid = u.id) WHERE MATCH(lastname, firstname, location, created) AGAINST('%$search%' IN BOOLEAN MODE)";