2013-10-31 90 views
2

我有以下搜索腳本,但它不工作100%。我的目標是讓4個文本框在一個名爲users的表中搜索記錄,並且只有在提交了搜索按鈕之後,該表必須顯示,但是在頁面加載表頭時,如果搜索一個記錄它顯示我的所有記錄,這是在我的表,而不是僅僅記錄的是我在尋找Php搜索不工作100%

任何建議將是非常有益的

<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> 
+0

您正在打印表頭/標題在用戶搜索到任何東西之前。把它放在'<?php if($ _ POST ['button']){}?> – Darren

回答

0

你會如何期待這個:

else { 
    $sql = "SELECT * FROM users WHERE id>0".$search_first.$search_last.$search_email.$search_company; 
} 
在聲明中 IF

的$ search_company我還覺得else甚至不會因爲

$sql = "SELECT * FROM users WHERE id>0".$search_first.$search_last.$search_email.$search_company;

止跌需要你的情況

會甚至工作如果不執行IF語句

1

請刪除else條件,

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;