AliceB1 AliceB1 -4 years ago 62
PHP Question

simple SQL search form not working

I have a really basic search form that does what i need it to do

<form action="" method="post">
<p>Please enter a search term below</p>
<input type="text" name="search">
<input type="submit" name="submit">
if (isset($_POST["submit"])) {
$search = $_POST["search"];
$con = mysqli_connect("", "", "", "");
if ($con->connect_error) {
echo 'Connection Failed: ' . $con->connect_error;
} else {
$sql = "select * from completerecipe where name like '%$search%' && prep <= 50";
$res = $con->query($sql);

while ($row = $res->fetch_assoc()) {
echo 'name: ' . $row["name"] . ' <br>';

This basic code worked perfectly so I have changed it to allow me to add multiple fields and search however it doesn't work. I am hoping that it is something simple but it has been tripping me up for literal hours. If anyone has any ideas just by looking at the below, I would appreciate the help. I don't study computing or anything, just wanted to pick up a hobby. Thanks in advance, Alice.

<form action="" method="post">
<p> Filter by: </p>

<label for="name">Recipe name:</label>
<input type="text" name="name" class="form-control"><br>

<label for="prep">Prep time under:</label>
<select class="form-control" name="prep">

<label for="cook">Cook time under:</label>
<select class="form-control" name="cook">
<label for="serves">Number of servings:</label>
<select class="form-control" name="serves">

<label for="age">Aged from:</label>
<select class="form-control" name="age">


<label for="name">Kcal under:</label>
<input type="text" class="form-control" name="kcal" length="5">

<label for="name">Fat under:</label>
<input type="text" class="form-control" name="fat" length="5">
<label for="name">Saturates under:</label>
<input type="text" class="form-control" name="saturates" length="5">
<label for="name">Carbs under:</label>
<input type="text" class="form-control" name="carbs" length="5">
<label for="name">Sugars under:</label>
<input type="text" class="form-control" name="sugars" length="5">
<label for="name">Fibre under:</label>
<input type="text" class="form-control" name="fibre" length="5">
<label for="name">Protein under:</label>
<input type="text" class="form-control" name="protein" length="5">
<label for="name">Salt under:</label>
<input type="text" class="form-control" name="salt" length="5">

<input type="submit" name="submit"> </form>

$con = mysqli_connect("","","","");
echo 'Connection Failed: '.$con->connect_error;
$sql="select * from completerecipe where name like '%$name%' && prep >= '%$prep%' && cook >= '%$cook%' && serves >= '%$serves%' && age >= '%$age%' && kcal >= '%$kcal%' && fat >= '%$fat%' && saturates >= '%$saturates%' && carbs >= '%$carbs%' && sugars >= '%$sugars%' && fibre >= '%$fibre%' && protein >= '%$protein%' && salt >= '%$salt%'";

echo 'name: '.$row["name"].' <br>';

Upon clicking submit, nothing appears even with the correct details that I know def match my database

Answer Source

Your $sql query is not going to work. Try below instead. I removed the % signs when your looking for values to be greater than...

$sql="SELECT * FROM completerecipe WHERE name LIKE '%$name%' AND prep >= '$prep' AND cook >= '$cook' AND serves >= '$serves' AND age >= '$age' AND kcal >= '$kcal' AND fat >= '$fat' AND saturates >= '$saturates' AND carbs >= '$carbs' AND sugars >= '$sugars' AND fibre >= '$fibre' AND protein >= '$protein' AND salt >= '$salt'";

And you probably have your database credentials set here??

$con = mysqli_connect("host", "user", "pass", "db")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download