Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: Save recipe: how to retrieve recipe name from previous page (it's in a table) and store in a php variable?
    text
    copied!<p>What I am trying to do is retrieve the recipe name from the previous page so I can use it in an SQL query.</p> <p>The code to display the table is below:</p> <pre><code>$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 "&lt;table border='1' width='100%'&gt; &lt;tr&gt; &lt;th&gt;Recipe Name&lt;/th&gt; &lt;th&gt;Recipe Price&lt;/th&gt; &lt;th&gt;Recipe Calories&lt;/th&gt; &lt;th&gt;Amount of Fat&lt;/th&gt; &lt;th&gt;Amount of Cholestrol&lt;/th&gt; &lt;th&gt;Amount of Carbs&lt;/th&gt; &lt;th&gt;Amount of Protein&lt;/th&gt; &lt;th&gt;Amount of Fibre&lt;/th&gt; &lt;th&gt;Amount of Sodium&lt;/th&gt; &lt;th&gt;Amount of Potassium&lt;/th&gt; &lt;th&gt;Recipe Source&lt;/th&gt; &lt;/tr&gt;"; while($row = mysqli_fetch_array($data)) { echo "&lt;tr&gt;"; echo "&lt;td&gt;" . $row['recipe_name'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['recipe_price'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['recipe_calories'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['recipe_fat'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['recipe_cholestrol'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['recipe_carbs'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['recipe_protein'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['recipe_fibre'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['recipe_sodium'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['recipe_potassium'] . "&lt;/td&gt;"; echo "&lt;td&gt;&lt;a href=\"" . $row['recipe_source'] . "\"&gt;Click here to view the recipe&lt;/a&gt;&lt;/td&gt;"; echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; $recipe_name = $row['recipe_name']; echo $recipe_name; </code></pre> <p>The code to save the recipe (so far):</p> <p>if (!isset($_SESSION['recipe_id'])) { if(isset($_POST['submit'])){</p> <pre><code> $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) { $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']; } } </code></pre> <p>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)</p> <p>Thanks for all the help in advance,</p> <p>Sarah</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload