karl_009 Posted February 10, 2009 Share Posted February 10, 2009 Hi, I found some code on the internet and I have been trying to change it to work a little differently. I have added a second drop down menu to the code and added parts to the SQL query but it always returns the same error; Notice: Undefined variable: dropdown1 in C:\wamp\cmstesting\test1.php on line 87 Notice: Undefined variable: search1 in C:\wamp\cmstesting\test1.php on line 87 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like ''' at line 1 Here is the code: <html> <head> <basefont face="Arial"> </head> <body> <?php error_reporting(E_ALL); if (!isset($_POST['Submit'])) { // form not submitted ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> search <input type="text" name="search"> <select size="1" name="dropdown"> <option value="" selected>search By...</option> <option value="first">First Name</option> <option value="last">Last Name</option> <option value="company">Company</option> <option value="address">Street Name</option> <option value="town">Town</option> <option value="city">City</option> <option value="postcode">Postcode</option> <option value="mobile">Mobile Number</option> <option value="phone">Phone Number</option> <option value="email">Email Address</option> <option value="website">Web Address</option> </select> <br> search1 <input type="text" name="search1"> <select size="1" name="dropdown1"> <option value="" selected>search By...</option> <option value="first">First Name</option> <option value="last">Last Name</option> <option value="company">Company</option> <option value="address">Street Name</option> <option value="town">Town</option> <option value="city">City</option> <option value="postcode">Postcode</option> <option value="mobile">Mobile Number</option> <option value="phone">Phone Number</option> <option value="email">Email Address</option> <option value="website">Web Address</option> </select> <br> <input type="Submit" value="Submit" name="Submit"> </form> <?php } else { // Server Variables $host = "localhost"; $user = "root"; $pass = "……."; $db = "test"; $search = empty($_POST['search'])? die ("ERROR: Enter search Criteria") : mysql_escape_string($_POST['search']); $dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown']); // Open Connection $connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host"); //Select Database mysql_select_db($db) or die ("Unable to connect to database"); //Create query $query = "SELECT * FROM contacts WHERE $dropdown like'$search' AND $dropdown1 like '$search1'" or die (mysql_error()); $result = mysql_query($query) or die (mysql_error()); $num=mysql_numrows($result); mysql_close($connect); echo "<b><center>Database Output</center></b><br><br>"; $i=0; while ($i < $num) { $first=mysql_result($result,$i,"first"); $last=mysql_result($result,$i,"last"); $company=mysql_result($result,$i,"company"); $address=mysql_result($result,$i,"address"); $town=mysql_result($result,$i,"town"); $city=mysql_result($result,$i,"city"); $postcode=mysql_result($result,$i,"postcode"); $mobile=mysql_result($result,$i,"mobile"); $phone=mysql_result($result,$i,"phone"); $email=mysql_result($result,$i,"email"); $website=mysql_result($result,$i,"website"); echo "<b>$first $last</b><br>Company: $company<br><br>Street Name: $address<br>Town: $town<br>City: $city<br>Postcode: $postcode<br>Mobile: $mobile<br>Phone: $phone<br>Email: $email<br>Website: $website<br><hr><br>"; $i++; } } ?> </body> </html> I have also tried this; $query = "SELECT * FROM contacts WHERE $dropdown='$search' AND $dropdown1='$search1'" or die (mysql_error()); Many thanks for any help… Karl Quote Link to comment https://forums.phpfreaks.com/topic/144655-solved-php-mysql-search-query-to-search-selected-columns-from-drop-down-menus/ Share on other sites More sharing options...
karl_009 Posted February 10, 2009 Author Share Posted February 10, 2009 Hey, I found the solution; <html> <head> <basefont face="Arial"> </head> <body> <?php error_reporting(E_ALL); if (!isset($_POST['Submit'])) { // form not submitted ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> search <input type="text" name="search"> <select size="1" name="dropdown"> <option value="" selected>search By...</option> <option value="first">First Name</option> <option value="last">Last Name</option> <option value="company">Company</option> <option value="address">Street Name</option> <option value="town">Town</option> <option value="city">City</option> <option value="postcode">Postcode</option> <option value="mobile">Mobile Number</option> <option value="phone">Phone Number</option> <option value="email">Email Address</option> <option value="website">Web Address</option> </select> <br> search1 <input type="text" name="search1"> <select size="1" name="dropdown1"> <option value="" selected>search By...</option> <option value="first">First Name</option> <option value="last">Last Name</option> <option value="company">Company</option> <option value="address">Street Name</option> <option value="town">Town</option> <option value="city">City</option> <option value="postcode">Postcode</option> <option value="mobile">Mobile Number</option> <option value="phone">Phone Number</option> <option value="email">Email Address</option> <option value="website">Web Address</option> </select> <br> <input type="Submit" value="Submit" name="Submit"> </form> <?php } else { // Server Variables $host = "localhost"; $user = "root"; $pass = "kayla"; $db = "test"; $search = empty($_POST['search'])? die ("ERROR: Enter search Criteria") : mysql_escape_string($_POST['search']); $dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown']); // NEEDED TO ADD THESE LINES IN $search1 = empty($_POST['search1'])? die ("ERROR: Enter search Criteria") : mysql_escape_string($_POST['search1']); $dropdown1 = empty($_POST['dropdown1'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown1']); // Open Connection $connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host"); //Select Database mysql_select_db($db) or die ("Unable to connect to database"); //Create query // NEEDED TO CHANGE THE AND TO AN OR... $query = "SELECT * FROM contacts WHERE $dropdown='$search' OR $dropdown1='$search1'" or die (mysql_error()); $result = mysql_query($query) or die (mysql_error()); $num=mysql_numrows($result); mysql_close($connect); echo "<b><center>Database Output</center></b><br><br>"; $i=0; while ($i < $num) { $first=mysql_result($result,$i,"first"); $last=mysql_result($result,$i,"last"); $company=mysql_result($result,$i,"company"); $address=mysql_result($result,$i,"address"); $town=mysql_result($result,$i,"town"); $city=mysql_result($result,$i,"city"); $postcode=mysql_result($result,$i,"postcode"); $mobile=mysql_result($result,$i,"mobile"); $phone=mysql_result($result,$i,"phone"); $email=mysql_result($result,$i,"email"); $website=mysql_result($result,$i,"website"); echo "<b>$first $last</b><br>Company: $company<br><br>Street Name: $address<br>Town: $town<br>City: $city<br>Postcode: $postcode<br>Mobile: $mobile<br>Phone: $phone<br>Email: $email<br>Website: $website<br><hr><br>"; $i++; } } ?> </body> </html> Thanks Karl Quote Link to comment https://forums.phpfreaks.com/topic/144655-solved-php-mysql-search-query-to-search-selected-columns-from-drop-down-menus/#findComment-759070 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.