您好我有一个搜索页面,其中包含下拉菜单,文本字段和提交按钮。下拉菜单有三个项目,一个是Property ID,Property Deal Type,第三个是Property Status。
我希望用户选择这三个中的任何一个并从下拉到文本框中输入所选项目的文本,然后按下按钮并将其搜索到数据库中,但不幸的是我创建的查询列出了所有记录
问题出在哪里?
这是我的代码:
<html>
<head>
</head>
<body>
<form action="" method="post">
<table width="1330" border="0" align="center" bordercolor="#000000">
<th colspan="13"><font size="+3">Search Page</font></th></tr>
<tr><th colspan="13">
<select name="searchbox" class="dropdown">
<option value="" selected disabled>Search Criteria</option>
<option value="PropertyID">Search By ID</option>
<option value="PropertyDealType">Search By DealType</option>
<option value="PropertyStatus">Search By Status</option>
</select>
<input type="text" name="searchtext" class="search">
<input type="submit" id="Submitbutton" value="Search"><br >
<?php
require_once('../db.php');
@$searchtext = $_POST['searchtext'];
$query = "SELECT
properties.PropertyID,
properties.PropertyType,
properties.PropertyDealType,
properties.PropertyStatus,
remoteemployees.RemoteEmployeeFullName,
propertyowners.PropertyOwnerName,
propertydealers.PropertyDealerName
FROM remoteemployees,
propertyowners,
propertydealers,
properties
WHERE
properties.PropertyOwnerID=propertyowners.PropertyOwnerID
AND properties.PropertyDealerID=propertydealers.PropertyDealerID
AND remoteemployees.RemoteEmployeeEmail=properties.RemoteEmployeeEmail
OR properties.PropertyDealType = '$searchtext'
OR properties.PropertyID = '$searchtext'
OR properties.PropertyStatus = '$searchtext'";
$query_run = $connection->query($query);
if($query_run->num_rows == 0)
{
exit();
} ?>
</th></tr>
<tr><th>PropertyID</th><th>Property Type</th><th>Property Deal Type</th><th>Property Owner</th><th>Property Dealer</th>
<th>Remote Emp Name</th><th>Property Status</th><th>Active</th><th>Inactive</th>
</tr>
<?php
if( $connection->error ) exit( $connection->error );
while($row=$query_run->fetch_assoc())
{
?>
<tr><td><?php echo $row['PropertyID'] ?></td><td><?php echo $row['PropertyType'] ?></td><td><?php echo $row['PropertyDealType'] ?></td><td><?php echo $row['PropertyOwnerName'] ?></td><td><?php echo $row['PropertyDealerName'] ?></td>
<td><?php echo $row['RemoteEmployeeFullName'] ?></td><td><?php echo $row['PropertyStatus'] ?></td><td><a href="Property active Page execution.php?PropertyID=<?php echo $row['PropertyID'] ?>">Active Property</a></td><td><a href="Property Inactive Page execution.php?PropertyID=<?php echo $row['PropertyID'] ?>">Inactive Property</a></td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>
答案 0 :(得分:0)
试试这个,
$query = "SELECT
properties.PropertyID,
properties.PropertyType,
properties.PropertyDealType,
properties.PropertyStatus,
remoteemployees.RemoteEmployeeFullName,
propertyowners.PropertyOwnerName,
propertydealers.PropertyDealerName
FROM remoteemployees,
propertyowners,
propertydealers,
properties
WHERE
properties.PropertyOwnerID=propertyowners.PropertyOwnerID
AND properties.PropertyDealerID=propertydealers.PropertyDealerID
AND remoteemployees.RemoteEmployeeEmail=properties.RemoteEmployeeEmail
AND ( properties.PropertyDealType = '$searchtext'
OR properties.PropertyID = '$searchtext'
OR properties.PropertyStatus = '$searchtext'");
答案 1 :(得分:0)
修改您的查询
$query = "SELECT
properties.PropertyID,
properties.PropertyType,
properties.PropertyDealType,
properties.PropertyStatus,
remoteemployees.RemoteEmployeeFullName,
propertyowners.PropertyOwnerName,
propertydealers.PropertyDealerName
FROM remoteemployees,
propertyowners,
propertydealers,
properties
WHERE
properties.PropertyOwnerID=propertyowners.PropertyOwnerID
AND properties.PropertyDealerID=propertydealers.PropertyDealerID
AND remoteemployees.RemoteEmployeeEmail=properties.RemoteEmployeeEmail
OR properties.{$_POST['searchbox']} = '$searchtext'";
因为您将从下拉列表中收到字段名称,所以只需使用它即可。
有意地,您应该使用表JOINS
而不是从具有条件的多个表中进行选择。还应该在字符串比较中使用LIKE
关键字。