Jump to content

Having Trouble Joining Some Tables.


WD1812

Recommended Posts

Even if the URL ends with recipeID=Blueberry%20Pie, I still get the recipe for Apple Strudel.
If you have the following code to create the link:<?php do { ?><li><a href="../Recipes/recipesdesserts.php?recipeID=<?php echo $row_GetRecipes['recipe_id']; ?>"><strong><?php echo $row_GetRecipes['Title']; ?></strong></a></li><?php } while ($row_GetRecipes = mysql_fetch_assoc($GetRecipes)); ?>The URL should display: recipeID=2If it doesn't you still have something not right when you create the link. If it does display that, and you still get the recipe for the Apple strudel you need to make sure you add the WHERE clause to your query like Justsomeguy showed you before.
$query_GetRecipes = "SELECT Recipes.Title, Recipes.Ingredients, Recipes.Prep, Recipes.Serves FROM Recipes WHERE Recipes.ID=" . GetSQLValueString($_GET['recipeID'], 'text');

Link to comment
Share on other sites

When you get the records to show on the page, make sure you also get the recipe_id column. This is what the query looks like now:$query_GetRecipes = "SELECT Recipes.Title, Recipes.Ingredients, Recipes.Prep, Recipes.Serves FROM Recipes";So it's not even getting the ID or recipe_id columns, which is why those are blank when you try to print them. If you want to get all fields instead of listing them you can use *:$query_GetRecipes = "SELECT * FROM Recipes";

Link to comment
Share on other sites

When you get the records to show on the page, make sure you also get the recipe_id column. This is what the query looks like now:$query_GetRecipes = "SELECT Recipes.Title, Recipes.Ingredients, Recipes.Prep, Recipes.Serves FROM Recipes";So it's not even getting the ID or recipe_id columns, which is why those are blank when you try to print them. If you want to get all fields instead of listing them you can use *:$query_GetRecipes = "SELECT * FROM Recipes";
When I entered $query_GetRecipes = "SELECT * FROM Recipes"; WHERE Recipes.ID=" . GetSQLValueString($_GET['recipeID'], 'text');the background image on the page was completely removed.When I entered just$query_GetRecipes = "SELECT * FROM Recipes";and previewed in a browser, the numbers from the ordered list are there, but the titles are goneI left this code as is:<a href="../Recipes/recipesdesserts.php?recipeID=<?php echo $row_GetRecipes['Title']; ?>"><strong><?php echo $row_GetRecipes['Title']; ?></strong></a></li><?php } while ($row_GetRecipes = mysql_fetch_assoc($GetRecipes)); ?>Replaced:$query_GetRecipes = "SELECT Recipes.Title, Recipes.Ingredients, Recipes.Prep, Recipes.Serves FROM Recipes";with:$query_GetRecipes = "SELECT Recipes.Title, Recipes.Ingredients, Recipes.Prep, Recipes.Serves FROM Recipes WHERE Recipes.ID=" . GetSQLValueString($_GET['recipeID'], 'text');Previewed in a browser, and the recipes tiles that were there had disappeared.I then changed <?php echo $row_GetRecipes['Title']; ?>"> to <?php echo $row_GetRecipes['recipe_id']; ?>">and the recipe titles were still gone.I tried entering the apple info again, and got:Duplicate entry 'Apple' for key 'title'
Link to comment
Share on other sites

When you create the links for the recipes your query should look like this:

$query_GetRecipes = "SELECT * FROM Recipes";

That will pull all the recipes in the Recipes table. Then when you actually write the links it should look like this:

<?php do { ?><li><a href="../Recipes/recipesdesserts.php?recipeID=<?php echo $row_GetRecipes['recipe_id']; ?>"><strong><?php echo $row_GetRecipes['Title']; ?></strong></a></li><?php } while ($row_GetRecipes = mysql_fetch_assoc($GetRecipes)); ?>

Now when you click on the links it will submit the recipe ID to another page (../Recipes/recipesdesserts.php) and in that page you will need to have the following query to pull the recipe with the id matching the recipeID submitted in the query string of the URL:

$query_GetRecipes = "SELECT * FROM Recipes WHERE Recipes.ID=" . GetSQLValueString($_GET['recipeID'], 'text');

Link to comment
Share on other sites

Don't just copy and paste stuff and see what happens, you need to understand why it's doing what it is, what you're trying to do, and how to do it.

I left this code as is:<a href="../Recipes/recipesdesserts.php?recipeID=<?php echo $row_GetRecipes['Title']; ?>">
Why did you leave that code as it is? Why are you still printing the title as the recipe ID? Do you understand that the point is to click on a link which includes the recipe ID, and read that ID on the next page to show the rest of the information for that record?
Link to comment
Share on other sites

The concept of what you're trying to do is simple, maybe the Dreamweaver code is confusing you. The goal is to display a list of items in the database, click a link, and see details about it. This code will do that:

<?php require_once('../../Connections/clan_db.php');$result = mysql_query('SELECT recipe_id, Title FROM recipes');while ($row = mysql_fetch_assoc($result)){  echo '<a href="page2.php?id=' . $row['recipe_id'] . '">' . $row['Title'] . '<a><br>';}?>

That's all you need to show a list of items, that's it. On page2.php in order to show the rest of the information, this is all the code you need:

<?php require_once('../../Connections/clan_db.php'); $id = isset($_GET['id']) ? intval($_GET['id']) : 0;if ($id == 0){  echo 'No ID selected.';  exit();}$result = mysql_query('SELECT * FROM recipes WHERE recipe_id=' . $id);if ($row = mysql_fetch_assoc($result)){  echo 'Title: ' . $row['Title'] . '<br>';  echo 'Ingredients: ' . $row['Ingredients'] . '<br>';  echo 'Prep: ' . $row['Prep'] . '<br>';  echo 'Serves: ' . $row['Serves'];}else{  echo 'The record was not found in the database.';}?>

Study the first and second pages there and see how and why they work, and then apply that knowledge to the stuff Dreamweaver spit out.

Link to comment
Share on other sites

  • 3 weeks later...
The concept of what you're trying to do is simple, maybe the Dreamweaver code is confusing you. The goal is to display a list of items in the database, click a link, and see details about it. This code will do that:
<?php require_once('../../Connections/clan_db.php');$result = mysql_query('SELECT recipe_id, Title FROM recipes');while ($row = mysql_fetch_assoc($result)){  echo '<a href="page2.php?id=' . $row['recipe_id'] . '">' . $row['Title'] . '<a><br>';}?>

That's all you need to show a list of items, that's it. On page2.php in order to show the rest of the information, this is all the code you need:

<?php require_once('../../Connections/clan_db.php'); $id = isset($_GET['id']) ? intval($_GET['id']) : 0;if ($id == 0){  echo 'No ID selected.';  exit();}$result = mysql_query('SELECT * FROM recipes WHERE recipe_id=' . $id);if ($row = mysql_fetch_assoc($result)){  echo 'Title: ' . $row['Title'] . '<br>';  echo 'Ingredients: ' . $row['Ingredients'] . '<br>';  echo 'Prep: ' . $row['Prep'] . '<br>';  echo 'Serves: ' . $row['Serves'];}else{  echo 'The record was not found in the database.';}?>

Study the first and second pages there and see how and why they work, and then apply that knowledge to the stuff Dreamweaver spit out.

Thank you for the code!I'm still struggling. One thing that I'm not sure I noticed before, but the newest recipe always becomes the first recipe in the list. And as before, the info for the first recipe is the one that's always shown no matter which title I click on.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...