Stoty Posted November 6, 2012 Share Posted November 6, 2012 (edited) For one of my wife's xmas gifts, I'm turning our massive, beat up cookbook binder into an online cookbook so she can add/edit/search/delete recipes and use our iPad to do all this. I've run into a problem that I'm sure is very basic for some people on here, but I can't seem to get it to work. Basically I have a "view.php" page that displays all the recipes I've entered into mySQL database. What I've done is create a dynamic "recipe.php" page that will display all the recipe info for whichever recipe she clicked on, instead of creating a static page for EACH recipe (which would take forever to do). Essentially this is what I'm trying to do: 1) Pass the "ID" variable of the recipe from "view.php" to "recipe.php" 2) Populate all the fields (ingredients, cooking steps, etc) from the database, BASED on the ID from the "view.php" page. An example URL that is passed from "view.php" is mywebsite.com/cookbook/recipe.php?id=3 View.php code: Here is the loop I have to display all the recipes <?php while($row = mysql_fetch_array($sql_result)) { $id = $row["id"]; $title = $row["title"]; $category = $row["category"]; $subcategory = $row["subcategory"]; $subcategory2 = $row["subcategory2"]; $preptime = $row["preptime"]; $cooktime = $row["cooktime"]; $serves = $row["serves"]; echo "<tr>"; echo "<td class='display'> </td>"; echo "<td class='display'>$title</td>"; echo "<td class='display2'>$category</td>"; echo "<td class='display2'>$subcategory</td>"; echo "<td class='display2'>$subcategory2</td>"; echo "<td class='display2'>$preptime</td>"; echo "<td class='display2'>$cooktime</td>"; echo "<td class='display2'><form action='recipe.php' method='get'><button name='id' value='$id' type='submit'>View</button></form></td>"; echo "<td class='display'> </td>"; echo "</tr>"; } mysql_free_result($sql_result); ?> The code for the "recipe.php" page is: <?php $frmID = $_GET["id"]; $con = mysql_connect("localhost", "username", "password") or die("Could not connect to server"); $db = mysql_select_db("DB", $con) or die("Couldn't select database"); $result = mysql_query("SELECT * FROM tblCookbook where id='$frmID'"); while($row = mysql_fetch_array($result)) { ?> Displaying the info for the recipe: <tr> <td width="300" class="Title"> <?php echo $row['title']; ?> </td> <td> </td> <td> </td> </tr> <tr> <td width="55%" class="heading">Category:</td> <td width="45%" class="headerItem"><?php echo $row['category']; ?></td> </tr> Now, the thing that is puzzling me is that the "Title" for the dish will work, but that's it. Nothing else populates. (for example, "Category" from the above code snippet doesnt work, but the "Title" does. Any help would be MUCH appreciated! Edited November 6, 2012 by Stoty Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/ Share on other sites More sharing options...
Jessica Posted November 6, 2012 Share Posted November 6, 2012 Look at your data in a tool like phpMyAdmin. Is there a category value? (which, btw, should be in its own table and joined to this one, but let's work on one thing at a time) Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390631 Share on other sites More sharing options...
Zane Posted November 6, 2012 Share Posted November 6, 2012 Hmm.. try putting this little snippet in and post back here what you get $result = mysql_query("SELECT * FROM tblCookbook where id='$frmID'"); while($row = mysql_fetch_array($result)) { print "<pre>", print_r($row), "</pre>"; ?> Displaying the info for the recipe: Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390632 Share on other sites More sharing options...
Barand Posted November 6, 2012 Share Posted November 6, 2012 print "<pre>", print_r($row), "</pre>"; Needs to be print "<pre>" . print_r($row) . "</pre>"; // concatenated or use "echo" instead of "print" if there are several comma-separated arguments Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390638 Share on other sites More sharing options...
Stoty Posted November 6, 2012 Author Share Posted November 6, 2012 (edited) @Jessica - yes there is data in the database for the recipe's. I have only inputted three just to test to make sure that everything works. Edited November 6, 2012 by Stoty Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390640 Share on other sites More sharing options...
Barand Posted November 6, 2012 Share Posted November 6, 2012 Let me explain how this forum works. You want help. To help you we need information. We cannot see what you can see so we sometimes have to ask for specific information. In this case you were asked for specific output for us to see, not just confirmation that it is there. Help us and we help you. Don't help us and there is nothing we can do. Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390643 Share on other sites More sharing options...
Stoty Posted November 6, 2012 Author Share Posted November 6, 2012 I added the following: $result = mysql_query("SELECT * FROM tblCookbook where id='$frmID'"); while($row = mysql_fetch_array($result)) { print "<pre>" . print_r($row) . "</pre>"; } ?> Page came up blank.... Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390644 Share on other sites More sharing options...
Jessica Posted November 7, 2012 Share Posted November 7, 2012 You need the second argument of print_r, which is a Boolean, to use it that way. Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390650 Share on other sites More sharing options...
Barand Posted November 7, 2012 Share Posted November 7, 2012 Oops, missed that one Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390651 Share on other sites More sharing options...
Stoty Posted November 7, 2012 Author Share Posted November 7, 2012 I don't work with PHP much, so I'm not sure what you mean Jessica. Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390655 Share on other sites More sharing options...
Jessica Posted November 7, 2012 Share Posted November 7, 2012 php.net - familiarize yourself with it. Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390657 Share on other sites More sharing options...
Stoty Posted November 7, 2012 Author Share Posted November 7, 2012 lol.... gee thanks. Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390660 Share on other sites More sharing options...
jcbones Posted November 7, 2012 Share Posted November 7, 2012 Stoty, I'll explain. Your code must be amended to: $result = mysql_query("SELECT * FROM tblCookbook where id='$frmID'"); while($row = mysql_fetch_array($result)) { print "<pre>" . print_r($row,true) . "</pre>"; //true (boolean) must be added as a second parameter to the print_r() function. Else it will error out, which would be a blank page if display errors is turned off. } ?> Which leads to another de-bugging feature. Add these two lines to the VERY top of your script, right under <?php error_reporting(-1); //report all errors. ini_set('display_errors',1); //display reported errors on the screen. Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390661 Share on other sites More sharing options...
Stoty Posted November 7, 2012 Author Share Posted November 7, 2012 Thanks very much for explaining that to me! Glad to see that some people like to take the time to explain/help others. Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390663 Share on other sites More sharing options...
Jessica Posted November 7, 2012 Share Posted November 7, 2012 Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390664 Share on other sites More sharing options...
Stoty Posted November 7, 2012 Author Share Posted November 7, 2012 Page still comes up blank. Here is the updated code: <?php error_reporting(-1); //report all errors. ini_set('display_errors',1); //display reported errors on the screen. $frmID = $_GET["id"]; $con = mysql_connect("localhost", "username", "password") or die("Could not connect to server"); $db = mysql_select_db("DB", $con) or die("Couldn't select database"); $result = mysql_query("SELECT * FROM tblCookbook where id='$frmID'"); while($row = mysql_fetch_array($result)) { print "<pre>" . print_r($row,true) . "</pre>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390665 Share on other sites More sharing options...
Stoty Posted November 7, 2012 Author Share Posted November 7, 2012 I've managed to get the page to show up again, without errors, but once again... only the "Title" works, the rest is blank. Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390672 Share on other sites More sharing options...
Stoty Posted November 7, 2012 Author Share Posted November 7, 2012 I ended up figuring out the problem this morning. Thanks for those that took the time to help/explain things. Much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390883 Share on other sites More sharing options...
Zane Posted November 7, 2012 Share Posted November 7, 2012 (edited) So what was the problem. Enlighten those who end up at this thread in a search. Seeing as you never did post the results of the print_r function like I mentioned, we have no clue what was going wrong. Edited November 7, 2012 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/270378-displaying-database-info-based-on-_get-variable/#findComment-1390886 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.