Making a select option tag retrieve data from database and display data from another table

Hey I have a restaurant and a food table, food table(F_ID) has as a foreign key the restorant id (R_ID). I have used this code, the selected option tag is generated with the names of the restaurants but when I click on one restaurant I get this error: Notice : Undefined index: restorant in C:\xampp\htdocs\robinfood\filter.php on line 41

include "connection.php";
$conn = Connect();

<!DOCTYPE html>

<form method="post" action="filter.php">
$sql3 = "SELECT * FROM restaurants";
$result=mysqli_query($conn,$sql3) or die("nuk ka restorant");

$opt = "<select id='restorant' name='restorant'>";

while($row = mysqli_fetch_assoc($result)){
	//$opt .= "<option value='$row[R_ID]'>$row[name]</option>"; 
	$opt .= '<option value="'.$id .'">' .$name . '</option>';

$opt .="</select>";
<input type="submit" name="submit" value="Kliko"/>

 	<?php echo $opt; ?> 

//grumbullimi i te dhenave 
	$select_term = $_POST['restorant'];
	echo $select_term;
	$sql = "SELECT * FROM food WHERE F_ID = '$select_term'";
	$result= mysqli_query($conn,$sql) or die ("nuk ka ushqim");

		$output = "There was no search resutlts!";
		while($row = mysqli_fetch_assoc($result)){
				$name = $row['name'];
				$f_id = $row['F_ID'];

				$output .= "<div> ".$name."</div>";
			echo $output;



Hi there @kristindoni1. Welcome to the forums.

When you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

I have done it for you this time.

Edit: I don’t think we need the images either :slight_smile:

okay thank you for that :slight_smile:

1 Like


<input type="submit" name="submit" value="Kliko"/>

 	<?php echo $opt; ?> 

your <select> tag is after the form is closed, so it’s not sent to your processing script. Move it to before you close the form.

1 Like

I think i fixed it :smiley: thank you, even though the foods are still not appearing

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.