www.webdeveloper.com
Results 1 to 6 of 6

Thread: making a search bar-but stuck

  1. #1
    Join Date
    Mar 2013
    Posts
    6

    making a search bar-but stuck

    hi everyone,
    can any one help me with this code
    Code:
    <?php
    mysql_connect("localhost","root","") or die("could not connect");
    mysql_select_db("databasetest") or die("could not find database");
    $output = '';
    //collect
    if(isset($_POST['search'])){
    	$searchq = $_POST['search'];
    	$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
    	
    	$query = mysql_query("SELECT * FROM authors WHERE fname LIKE '%$searchq%$' OR lname LIKE '%$searchq%$'") or die("could not search");
    	$count = mysql_num_rows($query);
    	if($count == 0){
    		 $output = 'there was no search result!';
    		}else{
    			while($row = mysql_fetch_array($query)){
    			   $firstname = $row['fname'];
    			   $lastname  = $row['lname'];
    			  
    			    $output .= '<div> '.$firstname.' '.$lastname.' </div>';
    		 }
    		
    	   }
    	}
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>search</title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
     <form action="index.php" method="post">
        <input type="text" name="search" placeholder="search" />
        <input type="submit" value=">>"  /> 
      </form>
    
    <?php print("$output");?>
    </body>
    </html>
    please help
    1.when i try to search, ther are no results displayed.

  2. #2
    Join Date
    Feb 2014
    Location
    south africa
    Posts
    14
    can you try this and tell me what it gives you in the green block if you change the code to this :
    Code:
    <?php
      mysql_connect("localhost","root","") or die("could not connect");
      mysql_select_db("databasetest") or die("could not find database");
      
      $verboseMode = true;
      $output = ''; 
    
      if($verboseMode) echo '<div style="float:left; border:3px solid green; margin:50px 50px; padding:50px; color:green; font-family:Courier"><h1>VERBOSE MODE ON</h1>';
      if(isset($_POST['search'])){ 
    	$searchq = $_POST['search'];
    	$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);  
      
      if($verboseMode){
        print_r($_POST);
        echo '<br /><b>searchq before preg_replace =</b> '.$_POST['search'];  
        echo '<br /><b>searchq after preg_replace =</b> '.$searchq;
    	}
    	
      $query = mysql_query("SELECT * FROM authors WHERE fname LIKE '%$searchq%$' OR lname LIKE '%$searchq%$'") or die("could not search");
    	$count = mysql_num_rows($query);
    	if($count == 0){
    		 $output = 'there was no search result!';
    		}else{
    			while($row = mysql_fetch_array($query)){
    			   $firstname = $row['fname'];
    			   $lastname  = $row['lname'];
    			  
    			    $output .= '<div> '.$firstname.' '.$lastname.' </div>';
    		 }
    		
    	   }
    	}
      if($verboseMode) echo '</div><div style="clear:both">&nbsp;</div>';
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>search</title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
     <form action="testCode.php" method="post">
        <input type="text" name="search" placeholder="search" />
        <input type="submit" value=">>"  /> 
      </form>
    
    <?php print("$output");?>
    </body>
    </html>

  3. #3
    Join Date
    Mar 2013
    Posts
    6
    if i search a name this is what i get

    VERBOSE MODE ON
    Array ( [search] => jack )
    searchq before preg_replace = jack
    searchq after preg_replace = jack

  4. #4
    Join Date
    Feb 2014
    Location
    south africa
    Posts
    14
    okay great, so we have established that the post is working fine, can you run this now, this will tell us if the query to the database is running

    Code:
    <?php
      mysql_connect("localhost","root","") or die("could not connect");
      mysql_select_db("databasetest") or die("could not find database");
      
      $query = mysql_query("SELECT * FROM authors") or die("could not search");
      while($row = mysql_fetch_array($query,MYSQL_ASSOC)){
        print_r($row);
        echo '<hr />';
      }
    ?>

  5. #5
    Join Date
    Mar 2013
    Posts
    6
    thanks for the quick reply man , this is the output
    Array ( [fname] => jack [lname] => rogers )
    __________________________________________
    Array ( [fname] => mark [lname] => winter )
    __________________________________________
    Array ( [fname] => steve [lname] => buckner )
    __________________________________________
    Array ( [fname] => peter [lname] => snow )

  6. #6
    Join Date
    Mar 2013
    Posts
    6
    hey thanks for that, i found what was wrong.
    There is one more thing i cannot figure out how do i make the results of the database display as drop down list.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles