database - php search bar when I press submit with nothing in the search bar it shows all the data -
database - php search bar when I press submit with nothing in the search bar it shows all the data -
the search fusion works correctly if press submit nil in search bar shows data. how message show saying nil has been entered search bar ?.i new php.
<?php $mysql_host="host"; $mysql_database="database"; $mysql_user="username"; $mysql_password="password"; $dbconnect=@mysql_connect($mysql_host, $mysql_user, $mysql_password); // trys connect database if (!$dbconnect) { exit("an error has occurred - not connect database."); // if couldn't connect, allow user know } if(!mysql_select_db($mysql_database)) { exit("an error has occurred - not select database."); //if couldn't select db, allow user know } $output = ''; //collect if(isset($_post['search'])) { $searchq = $_post['search']; $searchq = preg_replace("#[^0-9a-z]#i","",$searchq); $query = mysql_query("select * people firstname '%" . $searchq . "%' or surname '" . $searchq . "';"); $count = mysql_num_rows($query); if($count == 0) { $output = 'there no search results!'; } else { while ($row = mysql_fetch_array($query)) { $fname = $row ['firstname']; $lname = $row ['surname']; $id = $row ['id']; $output .= '<div>'.$fname.' '.$lname.'</div>'; } } } ?> <html> <head> <title>search</title> </head> <body> <form action="index.php" method="post"> <input type="text" name="search" placeholder="search here......." /> <input type="submit" value="submit" /> </form> <?php print("$output");?> </body> </html>
if want avoid of results showing when submit form, need check @ beingness submitted in text input.
right now, if $searchq == ""
you're sql query search: where firstname '%%'
wildcard on anything.
in order prepare this, add together $_post['search'] != ""
initial condition.
<?php // create sure perform search , show results if there's search if(isset($_post['search']) && $_post['search'] != "") { $searchq = $_post['search']; $searchq = preg_replace("#[^0-9a-z]#i","",$searchq); $query = mysql_query("select * people firstname '%" . $searchq . "%' or surname '" . $searchq . "';"); $count = mysql_num_rows($query); if($count == 0) { $output = 'there no search results!'; } else { while ($row = mysql_fetch_array($query)) { $fname = $row ['firstname']; $lname = $row ['surname']; $id = $row ['id']; $output .= '<div>'.$fname.' '.$lname.'</div>'; } } }
now search when form has been submitted string search with!
php database search echo
Comments
Post a Comment