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">
</form>
<?php
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">
<option>5</option>
<option>10</option>
<option>15</option>
<option>20</option>
<option>30</option>
<option>40</option>
<option>50</option>
<option>60</option>
</select>

<label for="cook">Cook time under:</label>
<select class="form-control" name="cook">
<option>5</option>
<option>10</option>
<option>15</option>
<option>20</option>
<option>30</option>
<option>40</option>
<option>50</option>
<option>60</option>
<option>70</option>
<option>80</option>
<option>90</option>
</select>
<label for="serves">Number of servings:</label>
<select class="form-control" name="serves">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>

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


<!--kcal-->

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

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

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

<?php
if(isset($_POST["submit"]))
{
$name=$_POST["name"];
$prep=$_POST["prep"];
$cook=$_POST["cook"];
$serves=$_POST["serves"];
$age=$_POST["age"];
$kcal=$_POST["kcal"];
$fat=$_POST["fat"];
$saturates=$_POST["saturates"];
$carbs=$_POST["carbs"];
$sugars=$_POST["sugars"];
$fibre=$_POST["fibre"];
$protein=$_POST["protein"];
$salt=$_POST["salt"];
$con = mysqli_connect("","","","");
if($con->connect_error){
echo 'Connection Failed: '.$con->connect_error;
}else{
$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%'";

$res=$con->query($sql);
while($row=$res->fetch_assoc()){
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