0

我几乎可以完成这项工作,但现在我被正式卡住了。我正在尝试使搜索区域能够按名字、姓氏和身份证号进行搜索。但我无法让它在 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 firstname 当前在哪里 - 我希望它搜索名字、姓氏和 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>
4

3 回答 3

2

你的意思是这样的吗?

$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' or lastname like '%".$term."%' or id = ".$term;
于 2013-06-20T00:50:08.733 回答
0

我认为这会奏效

SELECT * FROM tablename WHERE (Column1 LIKE SearchedText OR Column2 LIKE SearchedText)
于 2013-06-20T00:51:18.983 回答
0

你想匹配所有给定的标准吗?

"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

于 2013-06-20T00:59:48.850 回答