2013-06-20 79 views
0

我幾乎有這個工作,但現在我正式卡住了。我試圖使搜索區域能夠通過名字,姓氏和身份證號碼進行搜索。但我不能讓它只使用一個這條線在MySQL中顯示所有的人:MySQL搜索和顯示多個表格

這裏是形式:

<form action="form.php" method="post"> 
<input type="text" name="term" /> 
<input type="submit" value="Submit" /> 
</form> 

,這裏是在form.php的我試圖站上罰球線從它的工作:

$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%'"; 

WHERE姓是目前 - 我想這個搜索名字,姓氏和ID - 不僅僅是名字。

我錯過了一些東西,但我不知道是什麼。任何幫助是極大的讚賞!

編輯:

下面是完整的代碼。當我注意到其他變量時,我想我會留下一些你們可能需要幫助我的東西。請看看整個代碼:

// Database Connection String 
$con = mysql_connect($db_hostname,$db_username,$db_password); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db($db_database, $con); 
?> 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title>SpeedZone Data Search</title> 
     <style type="text/css"> 
table { border-collapse:collapse; } 
table td, table th { border:1px solid black;padding:5px; } 
tr:nth-child(even) {background: #ffffff} 
tr:nth-child(odd) {background: #ff0000} 
</style> 
    </head> 
    <body> 
<form action="" method="post"> 
<input type="text" name="term" /> 
<input type="submit" value="Search" /> 
</form> 
<?php 
if (!empty($_REQUEST['term'])) { 

$term = mysql_real_escape_string($_REQUEST['term']);  

$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' OR lastname like '%".$term."%' OR id = ".$term; 
$r_query = mysql_query($sql); 

echo "<table border='1' cellpadding='5'>"; 
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Address</th> <th>City</th> <th>State</th> <th>Zip</th> <th>Phone</th> <th>DL</th> <th>Email</th> <th>Car and Controller</th> <th></th> <th></th></tr>"; 

// loop through results of database query, displaying them in the table 
     while ($row = mysql_fetch_array($r_query)){ 

       // echo out the contents of each row into a table 
       echo "<tr>"; 
       echo '<td>' . $row['id'] . '</td>'; 
       echo '<td>' . $row['firstname'] . '</td>'; 
       echo '<td>' . $row['lastname'] . '</td>'; 
       echo '<td>' . $row['address'] . '</td>'; 
       echo '<td>' . $row['city'] . '</td>'; 
       echo '<td>' . $row['st'] . '</td>'; 
       echo '<td>' . $row['zip'] . '</td>'; 
       echo '<td>' . $row['phone'] . '</td>'; 
       echo '<td>' . $row['dl'] . '</td>'; 
       echo '<td>' . $row['email'] . '</td>'; 
       echo '<td>' . $row['carcont'] . '</td>'; 
       echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>'; 
       // echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>'; 
       echo '<td><a href="delete.php?id=' . $row['id'] . '" onclick="return confirm(\'Confirm?\')">Delete</a></td>'; 
       echo "</tr>"; 
     } 

     // close table> 
     echo "</table>"; 

} 
?> 
    </body> 
</html> 

回答

2

你的意思是這樣嗎?

$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' or lastname like '%".$term."%' or id = ".$term; 
+0

使用該行:現在的ID進行搜索,但沒有名字或姓氏。 – user2503303

0

,我認爲這會工作

SELECT * FROM tablename WHERE (Column1 LIKE SearchedText OR Column2 LIKE SearchedText) 
+0

這給了錯誤,並沒有工作。 – user2503303

0

你想匹配ALL給定的標準?

"SELECT * FROM rcrentals WHERE firstname LIKE '%{$term}%' AND lastname like '%{$term}%' AND id = {$term}"; 

或者任何給定標準的

"SELECT * FROM rcrentals WHERE firstname LIKE '%{$term}%' OR lastname like '%{$term}%' OR id = {$term}"; 

https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like

+0

這使得沒有任何項目可搜索。我希望能夠在一個框中輸入搜索詞的任意一個,並在數據庫中搜索它們。名字,姓氏或ID,或者全部一起。 – user2503303

+0

對於任何給定的標準,您只能使用正在搜索的ID。如果我搜索名字或姓氏,它不起作用。 – user2503303

+0

@ user2503303這沒有意義。你能否在應用程序中打印上述語句輸出的確切字符串? –