What I am trying to do is retrieve the recipe name from the previous page so I can use it in an SQL query.
The code to display the table is below:
table{ margin: 10px 0; } Carbohydrates: BananasEverything you need to know about bananas
<?php session_start(); $dbc = mysqli_connect('X', 'root', 'X', 'X') or die("Error " . mysqli_error($dbc)); mysqli_set_charset($dbc, "utf8"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $query = "SELECT `recipe_name`, `recipe_price`, `recipe_calories`, `recipe_fat`, `recipe_cholestrol`, `recipe_carbs`, `recipe_protein`, `recipe_fibre`, `recipe_sodium`, `recipe_potassium`, `recipe_source` FROM `carbohydrates` WHERE `ingredient_name`= 'bananas' AND recipe_id = 1"; $data= mysqli_query($dbc,$query) or die('Query failed: ' . mysql_error()); echo "Recipe Name | Recipe Price | Recipe Calories | Amount of Fat | Amount of Cholestrol | Amount of Carbs | Amount of Protein | Amount of Fibre | Amount of Sodium | Amount of Potassium | Recipe Source |
---|---|---|---|---|---|---|---|---|---|---|
" . $row['recipe_name'] . " | "; echo "" . $row['recipe_price'] . " | "; echo "" . $row['recipe_calories'] . " | "; echo "" . $row['recipe_fat'] . " | "; echo "" . $row['recipe_cholestrol'] . " | "; echo "" . $row['recipe_carbs'] . " | "; echo "" . $row['recipe_protein'] . " | "; echo "" . $row['recipe_fibre'] . " | "; echo "" . $row['recipe_sodium'] . " | "; echo "" . $row['recipe_potassium'] . " | "; echo "Click here to view the recipe | "; echo "
Recipe Name | Recipe Price | Recipe Calories | Amount of Fat | Amount of Cholestrol | Amount of Carbs | Amount of Protein | Amount of Fibre | Amount of Sodium | Recipe Source |
---|---|---|---|---|---|---|---|---|---|
" . $row['recipe_name'] . " | "; echo "" . $row['recipe_price'] . " | "; echo "" . $row['recipe_calories'] . " | "; echo "" . $row['recipe_fat'] . " | "; echo "" . $row['recipe_cholestrol'] . " | "; echo "" . $row['recipe_carbs'] . " | "; echo "" . $row['recipe_protein'] . " | "; echo "" . $row['recipe_fibre'] . " | "; echo "" . $row['recipe_sodium'] . " | "; echo "Click here to view the recipe | "; echo "
and the code to save the recipe:
<?php error_reporting(E_ALL &~ E_NOTICE); // Start the session session_start(); // If the session vars aren't set, try to set them with a cookie if (!isset($_SESSION['user_id'])) { if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) { $_SESSION['user_id'] = $_COOKIE['user_id']; $_SESSION['username'] = $_COOKIE['username']; } } $user_id = $_SESSION['user_id']; echo $user_id; // Make sure the browser is transmitting in UTF-8 header('Content-type: text/html; charset=utf-8'); // Clear the error message $error_msg = ""; $dbc = mysqli_connect('localhost', 'root', 'root', 'help_me_be_healthy') or die("Error " . mysqli_error($dbc)); mysqli_set_charset($dbc, "utf8"); echo popopopop; if (!isset($_SESSION['recipe_id'])) { if(isset($_POST['submit'])){ echo success; $query = "SELECT `recipe_id` FROM `carbohydrates` WHERE `recipe_name` = \"banana bread\""; $data= mysqli_query($dbc,$query) or die('Query failed: ' . mysql_error()); if (mysqli_num_rows($data) == 1) { echo succcccess; $row = mysqli_fetch_assoc($data); $_SESSION['recipe_id'] = $row['recipe_id']; setcookie('recipe_id', $row['recipe_id'], time() + (60 * 60 * 24 * 30)); // expires in 30 days } echo $_SESSION['recipe_id']; } } ?>So for the SQL query above, I want to use ‘$recipe_name’ instead of using the actual name of the recipe (this does work but I want to be able to use this code for other recipes not just one)
Thanks for all the help in advance,
Sarah