Jump to content

Searching and Displaying Results in PHP


behrk2

Recommended Posts

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!

Link to comment
Share on other sites

<?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";
  }
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.