我在查询时遇到问题。我想要做的是检查每个变量是否存在,如果不存在则忽略它们。我也想在一个表格中显示结果。任何帮助将不胜感激!根据用户输入写入包含变量WHERE的查询
我到目前为止:这是我的代码。目前,它返回一个包含数据库所有结果的数组,但如果我将WHERE子句中的OR更改为AND,则需要填写所有字段。我希望用户能够输入尽可能多的信息,以便显示所有可能的结果。
<?php require("common.php");?>
<?php
if(!empty($_POST))
{
$sth = $db->prepare("SELECT * FROM customer WHERE First_name LIKE '%$First_name%' OR Surname LIKE '%$Surname%' OR DOB LIKE '$DOB' OR Street LIKE '%$Street%' OR Suburb LIKE '$Suburb' OR State LIKE '$State' OR Postcode LIKE '$Postcode' OR Phone LIKE '$Phone'");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
$First_name = $_POST['First_name'];
$Surname = $_POST['Surname'];
$DOB = $_POST['DOB'];
$Street = $_POST['Street'];
$Suburb = $_POST['Suburb'];
$State = $_POST['State'];
$Postcode = $_POST['Postcode'];
$Phone = $_POST['Phone'];
}
?>
<fieldset><legend>Find a customer</legend>
<form name="querycustomerform" method="post" action="qcustomer.php">
<table class="five">
<tr>
<td colspan="4">
<h3>Please fill out as many details as possible</h3>
</td>
</tr>
<tr>
<td>
<input type="text" name="First_name" maxlength="30" size="30" placeholder="First name">
</td>
<td>
<input type="text" name="Surname" maxlength="30" size="30" placeholder="Surname">
</td>
<td style="text-align: right">Date of Birth:</td>
<td>
<input type="date" name="DOB">
</td>
</tr>
<tr>
<td>
<input type="text" name="Street" maxlength="40" size="30" placeholder="Street Address">
</td>
<td>
<input type="text" name="Suburb" maxlength="15" size="30" placeholder="Suburb">
</td>
<td>
<select name="State">
<option value="">State</option>
<option value="ACT">Australian Capital Territory</option>
<option value="NSW">New South Wales</option>
<option value="NT">Northern Territory</option>
<option value="QLD">Queensland</option>
<option value="SA">South Australia</option>
<option value="TAS">Tasmania</option>
<option value="VIC">Victoria</option>
<option value="WA">Western Australia</option>
</select>
</td>
<td>
<input type="text" name="Postcode" maxlength="4" size="30" placeholder="Postcode">
</td>
</tr>
<tr>
<td>
<input type="text" name="Phone" maxlength="15" size="30" placeholder="Phone Number">
</td>
<td>
</td>
<td>
</td>
<td>
<input class="button" type="submit" value="Search">
</td>
</tr>
</table>
</form>
</fieldset>
检查一下填写的典型测试结果,如果添加的信息提供 – 2014-11-24 03:49:15
,并将该是什么样子查询? – 2014-11-24 03:52:51