Jump to content

Displaying Database Info Based On $_Get Variable


Stoty

Recommended Posts

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 by Stoty
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";

}

?>

Link to comment
Share on other sites

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.