Jump to content

Recommended Posts

I've been working on a project for the past few weeks and it's been a huge learning curve so I apologise in advance. 

 

The idea is to choose ingredients and you will retrieve recipes that contain them. Once the results show up the user can click to view in more detail. The user will then be taken to a page which will show the recipe name, image, ingredients and steps of that recipe. That is where I am currently stuck at.

 

r_YUIA.png

These are the results from a search, when you click look you will be directed to this page.

 

CF4_Sk.png

 

I don't know if this is right but this is the what I used for the form action.

echo '<form action="recipe.php?id=<?php echo $recipe_id;?>" method="POST">';
echo '<input id="search" name="Look" type="Submit" value="Look" >';
echo '</form>';

On the recipe page I have this php code at the top

<?php
mysql_connect("10.168.1.56", "cupboard_test", "***") or die("Connection Failed");
mysql_select_db("cupboard_test")or die("Connection Failed");


$result = mysql_query("SELECT `name` 
FROM `recipe` 
WHERE `id` = ".$_GET['id']." LIMIT 1");


?>

And I am trying to echo the name further down the page.

<?php 
echo $result['name'].'<br />'; 
?>

I know I have probably made quite a few mistakes here and I do apologise but it's all part of the learning experience.

 

 

Edited by Drumlegend
Link to comment
https://forums.phpfreaks.com/topic/277835-not-able-to-retrieve-id-from-database/
Share on other sites

Use this.

echo '<form action="recipe.php" method="POST">';
echo '<input type="hidden" name="id" value="$recipe_id">';
echo '<input id="search" name="Look" type="Submit" value="Look" >';
echo '</form>';

Values in querystring aren't POSTed

If you use Barand's suggestion, you'll need to use $_POST in your query instead of $_GET.

 

Note that you probably don't need a form for the Look buttons. You could get away with using a simple link and an image for the buttons.

 

 

Also, you'll want to make sure the ID is valid before running the query. Assuming the ID is a number, you could run it through the ctype_digit() function:

http://php.net/manual/en/function.ctype-digit.php

If you use Barand's suggestion, you'll need to use $_POST in your query instead of $_GET.

 

Note that you probably don't need a form for the Look buttons. You could get away with using a simple link and an image for the buttons.

 

 

Also, you'll want to make sure the ID is valid before running the query. Assuming the ID is a number, you could run it through the ctype_digit() function:

http://php.net/manual/en/function.ctype-digit.php

So I have changed the form to this.

 

echo '<form action="recipe.php" method="POST">';
echo "<a href='recipe.php?id=" . $recipe['id'] . "'>" . $recipe['name'] . "</a>";
echo '</form>';

There is no longer a button, but when clicking on the link I get this at the top of the url.

 

recipe.php?id=

 

It doesn't seem to be retrieving the ID

Edited by Drumlegend

So I have changed the form to this.

 

 

echo '<form action="recipe.php" method="POST">';
echo "<a href='recipe.php?id=" . $recipe['id'] . "'>" . $recipe['name'] . "</a>";
echo '</form>';

 

There is no longer a button, but when clicking on the link I get this at the top of the url.

 

recipe.php?id=

 

It doesn't seem to be retrieving the ID

 

If you're going to use the anchor tag, you don't need the form tag anymore. If you want the link to look like a button, you can create one as an image...or use CSS to style the text.

 

As for the ID, have you made sure that $recipe['id'] contains a value? You can use var_dump to find out:

http://php.net/manual/en/function.var-dump.php

 

Note that your original code used a different variable ($recipe_id).

If you're going to use the anchor tag, you don't need the form tag anymore. If you want the link to look like a button, you can create one as an image...or use CSS to style the text.

 

As for the ID, have you made sure that $recipe['id'] contains a value? You can use var_dump to find out:

http://php.net/manual/en/function.var-dump.php

 

Note that your original code used a different variable ($recipe_id).

I will take this into account once I've stopped pulling my hair out :P I think I need to do some more reading before I go any further, otherwise I will be stuck on this part for ages. Thank you :)

Edited by Drumlegend

 

<?php
$result = mysql_query("SELECT `name` 
FROM `recipe` 
WHERE `id` = ".$_GET['id']." LIMIT 1");
?>
And I am trying to echo the name further down the page.

 

<?php 
echo $result['name'].'<br />'; 
?>
I know I have probably made quite a few mistakes here and I do apologise but it's all part of the learning experience.

 

So I have changed the form to this.

 

 

 

echo '<form action="recipe.php" method="POST">';
echo "<a href='recipe.php?id=" . $recipe['id'] . "'>" . $recipe['name'] . "</a>";
echo '</form>';
There is no longer a button, but when clicking on the link I get this at the top of the url.

 

recipe.php?id=

 

It doesn't seem to be retrieving the ID

 

 

Which is it, you're trying to echo the name or the ID? Where is the code that creates $recipe?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.