Friday 31 August 2018

echo $ _GET ['id'] does not display anything

I have a page in which a user chooses a recipe which has a unique ID, they are then sent to another page where the information for that recipe is displayed but it is not displaying anything.

$recipes = mysql_query("
        SELECT DISTINCT `name`, `step1`, `step2`, `step3`, `image`, `reference`
        FROM `recipe` r
        INNER JOIN `recipe_ingredients` ri
        ON r.id = ri.recipe_id
        WHERE ri.ingredient_id IN (".$ingredient1.",".$ingredient2.",".$ingredient3.")
    ");

This is the query on the main page, this allows the user to choose a set of ingredients and then retrieve recipes from them.
echo '<form action="recipe.php?id=<?php echo $recipe_id;?>" method="POST">';
        echo '<input id="search" name="Look" type="Submit" value="Look" >';
        echo '</form>';

Under each recipe there is a button a user presses, this will send the user to the specific recipe.
$sql = mysql_query("SELECT `name`, `image`
FROM `recipe`
WHERE `id` = ".$_GET['id']." LIMIT 1");

This is the query I'm running on the results page (recipe.php)
Further down the page I ran this.
<?php
echo $_GET['id'];
?>

It didn't display an ID and in the url I was displayed with this:
"recipe.php?id="
There is data in the table so what am I doing wrong?

The following echo syntax is incorrect:
echo '<form action="recipe.php?id=<?php echo $recipe_id;?>" method="POST">';

should be:
echo '<form action="recipe.php?id='.$recipe_id.'" method="POST">';

I don't see where you are setting $recipe_id, which appears to be the root of your problem.
It is unclear where you are trying to retrieve the elusive $recipe_id from.
If your trying to pull it in from $recipes then you should include it in your sql statement:
SELECT `id`,....

then pull it in after your $recipes->fetch_assoc():
$recipe_id = $recipes['id'];

0 comments:

Post a Comment