behrk2 Posted October 18, 2007 Share Posted October 18, 2007 Hey guys, So I have the following html form set up on the page search1.php: <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input name="search" type="text" id="l_name7" size=25 maxlength=25> <select name="select"> <option value="r_num">R Number</option> <option value="f_name">First Name</option> <option value="l_name" selected>Last Name</option> <option value="reg_num">Region #</option> <option value="res_hall">Residence</option> <option value="username">Username</option> </select> <input type="Submit" name="Submit" value="Submit"> </form> So basically there is a text box (search) and a list box (select) containing the categories to search within the MySQL database. Now, in the same page, I have the following PHP Code: <?php $dbhost = "localhost"; $dbname = "hub_info"; $dbuser = "root"; $dbpass = "xxxxxx"; mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); //get the mysql and store in $result $result = mysql_query("SELECT * FROM ra_info WHERE '$select' LIKE '%$search%'"); //grab all the content while($r=mysql_fetch_array($result)) { $f_name=$r["f_name"]; $l_name=$r["l_name"]; //display the row echo "<br>$f_name <br> $l_name <br>"; } ?> Now, I have two problems. First, when I load the page (or submit a search), all of the results in the database are listed. I cannot narrow the results to only what I searched for. Second, the results are shown as soon as the page loads. Is there anyway to hide the PHP code until the search is actually performed? Thank you so much for your help! Quote Link to comment Share on other sites More sharing options...
trq Posted October 18, 2007 Share Posted October 18, 2007 <?php if (isset($_POST['Submit'])) { $dbhost = "localhost"; $dbname = "hub_info"; $dbuser = "root"; $dbpass = "xxxxxx"; $select = mysql_real_escape_string($_POST['select']); $search = mysql_real_escape_string($_POST['search']); mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); //get the mysql and store in $result if ($result = mysql_query("SELECT * FROM ra_info WHERE '$select' LIKE '%$search%'")) { if (mysql_num_rows($result)) { //grab all the content while ($r=mysql_fetch_assoc($result)) { $f_name = $r["f_name"]; $l_name = $r["l_name"]; //display the row echo "<br>$f_name <br> $l_name <br>"; } } } else { echo "No results found"; } } ?> Quote Link to comment 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.