php - Why isn't this form returning results? -
i have started trying learn php , mysql , have been following tutorials creating webpage search engine, have been experience issue wherein when submit form results aren't returned, have no idea problem lies or try , troubleshoot it, thought it'll worth shot post problem here. can me out, in advance.
php
<?php mysql_connect("localhost","root","123")or die("could not connect db"); mysql_select_db("members") or die("could not find db"); if(isset($_post['submit'])){ $searchq = $_post['submit']; $searchq = preg_replace("#[^0-9a-z]#i","",$searchq); $query = mysql_query("select * memberlist fname '%$searchq%' or lname '%$searchq%' ") or die(mysql_error()); $count = mysql_num_rows($query); if($count == 0){ $output = "no results found, sorry."; } else{ while($row = mysql_fetch_array($query)){ $firstname = $row['fname']; $lastname = $row['lname']; $output .= "<div>".$firstname." ".$firstname."</div>"; } } } ?>
html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>search</title> </head> <body> <form action="index.php" method="post"> <input type="text" name="searchfname" placeholder="enter first name"> <input type="text" name="searchlname" placeholder="enter last name"> <input type="submit" name="submit" value="submit"> </form> <?php print($output);?> </body> </html>
you can use $_post['submit']
check if form submitted, not hold form values. can access separate form values respective name.
so use $_post['searchfname']
value in first textbox , $_post['searchlname']
second.
your code should read more this;
$searchqf = $_post['searchfname']; $searchql = $_post['searchlname']; $searchqfreplace = preg_replace("#[^0-9a-z]#i","",$searchqf); $searchqlreplace = preg_replace("#[^0-9a-z]#i","",$searchql); $query = mysql_query("select * memberlist fname '%$searchqf%' or lname '%$searchql%' ") or die(mysql_error()); $count = mysql_num_rows($query);
notice way of composing queries insecure , vulnerable sql injection.
Comments
Post a Comment