Php搜索不工作100%
我有下面的搜索脚本,但它不是100%工作。我的目标是有4个文本框来搜索名为users的表中的记录,只有在提交搜索按钮后,才能显示该表,但是当页面加载时,第二个显示的是表格标题,如果搜索记录,它会显示我表格中的所有记录,而不仅仅是我正在搜索的记录 任何建议都会很有帮助Php搜索不工作100%,php,html,search,Php,Html,Search,我有下面的搜索脚本,但它不是100%工作。我的目标是有4个文本框来搜索名为users的表中的记录,只有在提交搜索按钮后,才能显示该表,但是当页面加载时,第二个显示的是表格标题,如果搜索记录,它会显示我表格中的所有记录,而不仅仅是我正在搜索的记录 任何建议都会很有帮助 <form id="form1" name="form1" method="post" action="View.php"> <label for="from">First Name</label>
<form id="form1" name="form1" method="post" action="View.php">
<label for="from">First Name</label>
<input name="first" type="text" id="first" size="10" value="<?php echo $_REQUEST["first"]; ?>" />
<label for="to">Last Name</label>
<input name="last" type="text" id="last" size="10" value="<?php echo $_REQUEST["last"]; ?>"/>
<label>Email:</label>
<input type="text" name="email" id="string" value="<?php echo stripcslashes($_REQUEST["email"]); ?>" />
<label>Company</label>
<select name="company">
<option value="">--</option>
<?php
include("config.php");
$sql = "SELECT * FROM users GROUP BY company ORDER BY company";
$sql_result = mysql_query ($sql, $dbConn ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["company"]."'".($row["company"]==$_REQUEST["company"] ? " selected" : "").">".$row["company"]."</option>";
}
?>
</select>
<input type="submit" name="button" id="button" value="Filter" />
</label>
<a href="View.php">
reset</a>
</form>
<br /><br />
<table width="700" border="1" cellspacing="0" cellpadding="4">
<tr>
<td width="90" bgcolor="#CCCCCC"><strong>First Name</strong></td>
<td width="95" bgcolor="#CCCCCC"><strong>Last Name</strong></td>
<td width="159" bgcolor="#CCCCCC"><strong>Company</strong></td>
<td width="191" bgcolor="#CCCCCC"><strong>Email</strong></td>
<td width="113" bgcolor="#CCCCCC"><strong>Contact Number</strong></td>
<td width="113" bgcolor="#CCCCCC"><strong>Position</strong></td>
<td width="113" bgcolor="#CCCCCC"><strong>How do you know the person</strong></td>
<td width="113" bgcolor="#CCCCCC"><strong>Comment</strong></td>
</tr>
<?php
if($_POST["button"])
{
if ($_REQUEST["first"]<>'') {
$search_first = " AND fname LIKE '%".mysql_real_escape_string($_REQUEST["fname"])."'";
}
if ($_REQUEST["last"]<>'') {
$search_last = " AND lname='".mysql_real_escape_string($_REQUEST["last"])."'";
}
if ($_REQUEST["email"]<>'') {
$search_email = " AND email='".mysql_real_escape_string($_REQUEST["email"])."'";
}
if ($_REQUEST["company"]<>'') {
$search_company = " AND company='".mysql_real_escape_string($_REQUEST["company"])."'";
}
else {
$sql = "SELECT * FROM users WHERE id>0".$search_first.$search_last.$search_email.$search_company;
}
$sql_result = mysql_query ($sql, $dbConn) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0)
{
while ($row = mysql_fetch_assoc($sql_result))
{
?>
<tr>
<td><?php echo $row["fname"]; ?></td>
<td><?php echo $row["lname"]; ?></td>
<td><?php echo $row["company"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td><?php echo $row["contactnumber"]; ?></td>
<td><?php echo $row["position"]; ?></td>
<td><?php echo $row["howdoyouknow"]; ?></td>
<td><?php echo $row["comment"]; ?></td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php
}
}
?>
</table>
名字
请删除其他条件
else {
$sql = "SELECT * FROM users WHERE id>0".$search_first.$search_last.$search_email.$search_company;
}
use like below i.e without else tag
$sql = "SELECT * FROM users WHERE id>0".$search_first.$search_last.$search_email.$search_company;
您如何期望:
else {
$sql = "SELECT * FROM users WHERE id>0".$search_first.$search_last.$search_email.$search_company;
}
当您在IF
另外,我确实认为,在您的案例中,甚至不需要else
,因为
$sql=“SELECT*FROM users WHERE id>0”。$search\u first.$search\u last.$search\u email.$search\u company代码>
如果不执行IF
语句,则无法工作在用户搜索任何内容之前,您正在打印表格标题。。将其放入