PHP: Save recipe: how to retrieve recipe name from previous page (it's in a tabl

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: Bananas

Everything 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 ""; while($row = mysqli_fetch_array($data)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } 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'] . "" . $row['recipe_price'] . "" . $row['recipe_calories'] . "" . $row['recipe_fat'] . "" . $row['recipe_cholestrol'] . "" . $row['recipe_carbs'] . "" . $row['recipe_protein'] . "" . $row['recipe_fibre'] . "" . $row['recipe_sodium'] . "" . $row['recipe_potassium'] . "Click here to view the recipe
"; $recipe_name = $row['recipe_name']; $_SESSION['recipe_name'] = $recipe_name; echo $recipe_name; mysqli_close($con); ?> <?php $dbc = mysqli_connect('localhost', 'root', 'root', 'help_me_be_healthy') 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_source` FROM `carbohydrates` WHERE `ingredient_name`= 'bananas' AND recipe_id = 2"; $data= mysqli_query($dbc,$query) or die('Query failed: ' . mysql_error()); echo ""; while($row = mysqli_fetch_array($data)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } 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'] . "" . $row['recipe_price'] . "" . $row['recipe_calories'] . "" . $row['recipe_fat'] . "" . $row['recipe_cholestrol'] . "" . $row['recipe_carbs'] . "" . $row['recipe_protein'] . "" . $row['recipe_fibre'] . "" . $row['recipe_sodium'] . "Click here to view the recipe
"; mysqli_close($con); ?>

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

The first thing you want to do is turn error reporting on (that is if you don’t have it already on).

[php]ini_set(‘display_errors’,1);
error_reporting(E_ALL);[/php]

The below needs the database connection to be setup and I didn’t test it out, meaning that it probably contains errors (syntax and maybe logical errors). However, I think you can get the gist of it and if you run into problems then http://www.php.net/ is a good resource to iron them out or here.

[php]<?php

function retrievRecipe($recipe_name) {

$recipe = array();
$query = “SELECT
recipe_id
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 recipe_name=? LIMIT 1”;
if ($stmt = $dbc->prepare($query)) {

/* bind parameters for markers */
$stmt->bind_param("s", $recipe_name);

/* execute query */
$stmt->execute();

/* bind result variables */
$stmt->bind_result($recipe['recipe_id'], $recipe['recipe_name'], $recipe['recipe_price'], $recipe['recipe_calories'], $recipe['recipe_fat'], $recipe['recipe_cholestrol'], $recipe['recipe_carbs'], $recipe['recipe_protein'], $recipe['recipe_fibre'], $recipe['recipe_sodium'], $recipe['recipe_potassium'], $recipe['recipe_source']);

/* fetch value */
$stmt->fetch();

$stmt->close();

return $recipe;

}
}

/* This is where a function will come in handy, then all you would have to do is */

$recipe_name = ‘Apple Pie’; // This could come from a user input variable from a form for example:

$output = retrievRecipe($recipe_name);

echo ‘

’;
print_r($output);
echo ‘
’;[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service