首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP和MySQL数据库一次搜索多个表

PHP和MySQL数据库一次搜索多个表
EN

Stack Overflow用户
提问于 2013-06-20 09:35:50
回答 2查看 756关注 0票数 0

我正在尝试按ID、名字和姓氏搜索表单。我希望用户在搜索字段中输入任意一个,然后从数据库中获取结果。

下面是我使用的实际表单:

代码语言:javascript
复制
<form action="form.php" method="post"> 
<input type="text" name="term" />
<input type="submit" value="Submit" /> 
</form> 

这是form.php

代码语言:javascript
复制
<?php
$db_hostname = 'localhost';
$db_username = 'test';
$db_password = 'test';
$db_database = 'test';

// 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="form.php" 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>

我现在有这样的功能:它只搜索ID,我希望能够输入ID,或者名字,或者姓氏,如果可能的话,或者名字和姓氏。

代码语言:javascript
复制
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;

我认为有一些事情我需要改变,但我很困惑,迷失在其中,无法解决它。请帮帮忙。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-20 09:43:29

当您将其与id进行比较时,您需要在$term两边加上引号。否则,如果不是数字,就会出现语法错误。

代码语言:javascript
复制
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' or lastname LIKE '%".$term."%' or id = '".$term."'";

此外,这还假设您不使用0作为id。当将数字与字符串进行比较时,字符串将被转换为数字,所有非数字字符串将被转换为0,并且它们将匹配该id。如果这是一个问题,你应该首先检查$term是否是一个数字。如果不是数字,则使用不包括id检查的查询:

代码语言:javascript
复制
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%$term%' or lastname LIKE '%$term%'";
if (is_numeric($term)) {
  $sql .= " or id = $term";
}
票数 1
EN

Stack Overflow用户

发布于 2013-06-20 09:47:48

你的问题在我看来很酷。尝试使用大括号分隔OR条件

$sql = "SELECT *SELECT* firstname WHERE firstname LIKE '%".$term."%‘or (lastname LIKE’%“lastname.”%‘) or (id = ".$term);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17203981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档