Drumlegend Posted May 9, 2013 Share Posted May 9, 2013 (edited) 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. These are the results from a search, when you click look you will be directed to this page. 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 May 9, 2013 by Drumlegend Quote Link to comment https://forums.phpfreaks.com/topic/277835-not-able-to-retrieve-id-from-database/ Share on other sites More sharing options...
Barand Posted May 9, 2013 Share Posted May 9, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/277835-not-able-to-retrieve-id-from-database/#findComment-1429297 Share on other sites More sharing options...
cyberRobot Posted May 9, 2013 Share Posted May 9, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/277835-not-able-to-retrieve-id-from-database/#findComment-1429305 Share on other sites More sharing options...
Drumlegend Posted May 9, 2013 Author Share Posted May 9, 2013 (edited) 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 May 9, 2013 by Drumlegend Quote Link to comment https://forums.phpfreaks.com/topic/277835-not-able-to-retrieve-id-from-database/#findComment-1429309 Share on other sites More sharing options...
cyberRobot Posted May 9, 2013 Share Posted May 9, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/277835-not-able-to-retrieve-id-from-database/#findComment-1429310 Share on other sites More sharing options...
Drumlegend Posted May 9, 2013 Author Share Posted May 9, 2013 (edited) 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 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 May 9, 2013 by Drumlegend Quote Link to comment https://forums.phpfreaks.com/topic/277835-not-able-to-retrieve-id-from-database/#findComment-1429312 Share on other sites More sharing options...
Jessica Posted May 10, 2013 Share Posted May 10, 2013 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/277835-not-able-to-retrieve-id-from-database/#findComment-1429414 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.