StressCase Posted November 16, 2009 Share Posted November 16, 2009 Hello everyone I need help creating a search form to query related tables I have a table called customer and a table called customercontact when the page loads if there is no search criteria I'm displaying all the customers with their respective contact my problem is when they enter a search criteria it reutrns all the contacts in the database for each result i.e. 50 contacts in the db and search returned to possible matches then the result is 100 and they all get named with criteria. I'm stuck on this any help would be greatly appreciated. $search = "-1"; if (!isset($_GET['searchtext'])) { $result = mysql_query("SELECT customer.CustomerID, customer.CompanyName, customer.Address, customercontact.FirstName, customercontact.LastName FROM customer, customercontact WHERE customercontact.CompanyID = customer.CustomerID") or die (mysql_error()); $row = mysql_fetch_assoc($result); } else { $search = $_GET['searchtext']; $result = mysql_query("SELECT customer.CustomerID, customer.CompanyName, customer.Address, customercontact.FirstName, customercontact.LastName FROM customer, customercontact WHERE CompanyName like '%" . $search . "%' OR MainTel like '%" . $search . "%' AND customer.CustomerID = customercontact.CompanyID ORDER BY CompanyName ASC") or die (mysql_error()); $row = mysql_fetch_assoc($result); } Link to comment https://forums.phpfreaks.com/topic/181680-searching-related-tables/ Share on other sites More sharing options...
StressCase Posted November 17, 2009 Author Share Posted November 17, 2009 Update.. I'm still stuck on this but I found if I remove one search criteria it works perfectly Hopefully someone knows hows I can add more search criteria. Thanks in advance. so now the code looks like this... <?php $search = "-1"; if (!isset($_GET['searchtext'])) { // If there is no search criteria Get all records $result = mysql_query("SELECT customer.CustomerID, customer.CompanyName, customer.Address, customercontact.FirstName, customercontact.LastName FROM customer, customercontact WHERE customercontact.CompanyID = customer.CustomerID") or die (mysql_error()); $row = mysql_fetch_assoc($result); } else { //assign search $search = $_GET['searchtext']; //query db based on the search criteria $result = mysql_query("SELECT customer.CustomerID, customer.CompanyName, customer.Address, customercontact.FirstName, customercontact.LastName FROM customer, customercontact WHERE customer.CompanyName like '%" . $search . "%' AND customer.CustomerID = customercontact.CompanyID ORDER BY CompanyName ASC") or die (mysql_error()); //return the search results $row = mysql_fetch_assoc($result); } ?> Link to comment https://forums.phpfreaks.com/topic/181680-searching-related-tables/#findComment-958903 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.