Reputation: 337
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?
Upvotes: 0
Views: 2644
Reputation: 4144
First of all before making any queries to the database make sure that all $variables that you use in the query are escaped, because you can get hacked very easily. Escape like this:
$_GET['variable'] = mysql_real_escape_string($_GET['variable']);
Integers can be escaped like this
$_GET['variable'] = (int) $_GET['variable'];
You are doing it wrong, you can add recipe_id as hidden input field and later read it after the form submittion. Here is example form.
<form action="/recipe.php" method="POST">
<input type="hidden" name="recipe_id" value="<?= $recipe_id ?>" />
<input id="search" name="Look" type="Submit" value="Look" />
</form>
And after the form is submitted you can do something like this:
$recipe_id = (int) $_POST['recipe_id'];
$sql = mysql_query("SELECT `name`, `image` FROM `recipe` WHERE `id` = $recipe_id LIMIT 1");
But I think you main problem is that $recipe_id in the form is just an empty variable, check that first :).
Upvotes: 1
Reputation: 13586
// change this
echo '<form action="recipe.php?id=<?php echo $recipe_id;?>" method="POST">';
// to this
echo '<form action="recipe.php?id=' . $recipe_id .'" method="POST">';
Change the line where you echo the form to something like this... You were trying to echo twice before (echo within the echo)... This works though.
Upvotes: -1
Reputation: 16828
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'];
Upvotes: 2