So I'm new to SQL, PHP and HTML. I'm trying to create a search system for my database that contains a table for foods and their prices. I've searched everywhere for people who have the same problem as me but none of the solutions seem to help me.
I've searched far and wide over all the forums I could find, loads of tutorials on YouTube yet to no avail.
Main PHP code:
<!doctype html>
<html>
<head>
<meta charset = "utf-8">
<title>Lists of foods and prices! Input yours here!</title>
</head>
<body>
<h1>Insert your fav food!</h1>
<table>
<form action = "" method = "post">
<tr>
<td>Food: </td><td><input type="text" name="Food"></td></tr>
<tr>
<td>Price: </td><td><input type="integer" name="Price"></td></tr>
<tr>
<td>Supermarket: </td><td><input type="text" name="Supermarket"></td></tr>
</tr>
<tr>
<td><input type="submit" name = "submit"></td></tr>
</form>
</table>
<table>
<form action = "" method = "post">
<tr>
<td>Search: </td><td><input type="text" name="Search"></td></tr>
<tr>
<td><input type="submit" name = "Search"></td></tr>
</form>
</table>
<?php
if(isset($_POST["submit"])){
include 'dbconfig.php';
include 'New Text Document.js';
$Food = $_POST['Food'];
$Price = $_POST['Price'];
$Supermarket = $_POST['Supermarket'];
mysqli_query($conn,"INSERT INTO costsoffood VALUES('$Food', '$Price', '$Supermarket')");
}
if(isset($_POST["Search"])){
include 'dbconfig.php';
$search = $_POST["Search"];
$query="SELECT * FROM costsoffood WHERE Food LIKE %$search%";
$result=mysqli_query($conn,"SELECT * FROM costsoffood WHERE Food LIKE '%$search%'");
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_array($result)){
echo "<tr><td>". $row['food']."</td><td>". $row['price']."</td><td>".$row['supermarket']."</td></tr>";
}
echo "</table>";
}
else{
echo "no results";
}
}
?>
</body>
</html>
dbconfig.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dataone";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>