doood Posted March 24, 2007 Share Posted March 24, 2007 This code works, but I don't know how to filter the results to where only one person is displayed..it displays all records with similar address or state or city, I would like to know how to make it display only one person when multiple search fields are entered. I'm using PHP5, and I have searched the forums but I'm not sure exactly if I found what I'm looking for...I don't know much about php or mysql. Any help would be appreciated. Thanks <?php include 'connect.php'; mysql_select_db("pserver_ITSE2302") or die(mysql_error()); $name = $_POST['name']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; if($name != "") $query = "SELECT * FROM lab5 WHERE name = '$name'"; if($address != "") $query = "SELECT * FROM lab5 WHERE address = '$address'"; if($city != "") $query = "SELECT * FROM lab5 WHERE city = '$city'"; if($state != "") $query = "SELECT * FROM lab5 WHERE state = '$state'"; if($zip != "") $query = "SELECT * FROM lab5 WHERE zip = '$zip'"; $result = mysql_query($query) or die (mysql_error()); if($result) { while($row = mysql_fetch_array($result)) { $name = $row['name']; $address = $row['address']; $city = $row['city']; $state = $row['state']; $zip = $row['zip']; echo "<table>"; echo "<tr>"; echo "<td>"; echo $name; echo "</td>"; echo "<td>"; echo $address; echo "</td>"; echo "<td>"; echo $city; echo "</td>"; echo "<td>"; echo $state; echo "</td>"; echo "<td>"; echo $zip; echo "</td>"; echo "</tr>"; echo "</table>"; } } else echo "Could not retrieve records: %s\n", mysql_error($connect); ?> Quote Link to comment https://forums.phpfreaks.com/topic/44076-solved-filtering-results-with-php-query/ Share on other sites More sharing options...
MadTechie Posted March 24, 2007 Share Posted March 24, 2007 try this <?php include 'connect.php'; mysql_select_db("pserver_ITSE2302") or die(mysql_error()); $name = $_POST['name']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $query = ""; if($name != "") $query .= "name = '$name' AND "; if($address != "") $query .= "address = '$address' AND "; if($city != "") $query .= "city = '$city' AND "; if($state != "") $query .= "state = '$state' AND "; if($zip != "") $query .= "zip = '$zip' AND "; $query = trim($query,"AND "); $FullQuery = "SELECT * FROM lab5 WHERE $query"; $result = mysql_query($FullQuery) or die (mysql_error()); if($result) { while($row = mysql_fetch_array($result)) { $name = $row['name']; $address = $row['address']; $city = $row['city']; $state = $row['state']; $zip = $row['zip']; echo "<table>"; echo "<tr>"; echo "<td>"; echo $name; echo "</td>"; echo "<td>"; echo $address; echo "</td>"; echo "<td>"; echo $city; echo "</td>"; echo "<td>"; echo $state; echo "</td>"; echo "<td>"; echo $zip; echo "</td>"; echo "</tr>"; echo "</table>"; } } else echo "Could not retrieve records: %s\n", mysql_error($connect); ?> Quote Link to comment https://forums.phpfreaks.com/topic/44076-solved-filtering-results-with-php-query/#findComment-214035 Share on other sites More sharing options...
doood Posted March 24, 2007 Author Share Posted March 24, 2007 alright! it worked. Thanks a lot I appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/44076-solved-filtering-results-with-php-query/#findComment-214042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.