Jack
Jack

Reputation: 1941

Why isn't this form returning results?

I have just started trying to learn PHP and MYSQL and have been following some tutorials for creating a webpage search engine, but have been experience an issue wherein when i submit the form the results aren't returned, i have no idea as to where the problem lies or where to try and troubleshoot it, so it thought it'll be worth a shot to post my problem here. Hopefully someone can help me out, thanks in advance.

PHP

<?php   

    mysql_connect("localhost","root","123")or die("Could not connect to 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 * FROM memberlist WHERE Fname LIKE '%$searchq%' OR Lname LIKE '%$searchq%'  ") or die(mysql_error());
    $count = mysql_num_rows($query);


 if($count == 0){
    $output = "No results were 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>

Upvotes: 0

Views: 112

Answers (5)

Eugine Joseph
Eugine Joseph

Reputation: 1558

Firstly,

either search with searchfname or with searchlname or both.

Secondly, modify like this after $count = mysql_num_rows($query);,

if($count == 0){
    $output = "No results were found, sorry.";
}
else{
    $output = '';
    while($row = mysql_fetch_array($query)){
        $firstname = $row['Fname'];
        $lastname = $row['Lname'];
        $output .= "<div>".$firstname." ".$firstname."</div>";
    }

Thirdly, Use print $output in the Second page(where database is fetched) and not in First page(Page with FORM).

If you want to show result in the First page, use jQuery/Ajax function

Upvotes: 0

Gerald Versluis
Gerald Versluis

Reputation: 34013

You can use $_POST['submit'] to check if the form was submitted, but it does not hold all the form values. You can access the separate form values by their respective name.

So use $_POST['searchfname'] for the value in the first textbox and $_POST['searchlname'] for the second.

Your code should read more like 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 * FROM memberlist WHERE Fname LIKE '%$searchqf%' OR Lname LIKE '%$searchql%'  ") or die(mysql_error());
$count = mysql_num_rows($query);

Notice that this way of composing queries is very insecure and vulnerable for SQL injection.

You're also asking for a way to troubleshoot. You probably want to look into echo and print_r.

Upvotes: 6

Volvox
Volvox

Reputation: 1649

In your code you are searching for 'submit' value instead of values from form.

Replace $searchq = $_POST['submit']; with:

$searchq = $_POST['searchfname'];
$searchq2 = $_POST['searchlname'];

and query:

Select * FROM memberlist WHERE Fname LIKE '%$searchq%' OR Lname LIKE '%$searchq2%'   

Upvotes: 0

Vishal Chawla
Vishal Chawla

Reputation: 57

you cannot use $searchq = $_POST['submit']; since no value is being posted whose name is submit

you must use any of the following....

$searchq = $_POST['searchfname'];

or

$searchq = $_POST['searchlname'];

Upvotes: 0

mocco
mocco

Reputation: 154

You have assigned the $searchq variable to your submit button. Change this line

$searchq = $_POST['submit'];

to

$searchq = $_POST['searchfname'];

or

$searchq = $_POST['searchlname'];

or both:

$searchq = $_POST['searchfname'].$_POST['searchlname'];

Upvotes: 0

Related Questions