PHP:saverecipe:如何从上一页检索配方名称(它位于表中)并存储在PHP变量中?

PHP:saverecipe:如何从上一页检索配方名称(它位于表中)并存储在PHP变量中?,php,mysqli,save,Php,Mysqli,Save,我试图做的是从上一页检索配方名称,以便在SQL查询中使用它 显示表格的代码如下所示: $query = "SELECT `recipe_name`, `recipe_price`, `recipe_calories`, `recipe_fat`, `recipe_cholestrol`, `recipe_carbs`, `recipe_protein`, `recipe_fibre`, `recipe_sodium`, `recipe_potassium`, `recipe_source` FR

我试图做的是从上一页检索配方名称,以便在SQL查询中使用它

显示表格的代码如下所示:

$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 "<table border='1' width='100%'>
<tr>
<th>Recipe Name</th>
<th>Recipe Price</th>
<th>Recipe Calories</th>
<th>Amount of Fat</th>
<th>Amount of Cholestrol</th>
<th>Amount of Carbs</th>
<th>Amount of Protein</th>
<th>Amount of Fibre</th>
<th>Amount of Sodium</th>
<th>Amount of Potassium</th>
<th>Recipe Source</th>
</tr>";

while($row = mysqli_fetch_array($data))
{
  echo "<tr>";
  echo "<td>" . $row['recipe_name'] . "</td>";
  echo "<td>" . $row['recipe_price'] . "</td>";
  echo "<td>" . $row['recipe_calories'] . "</td>";
  echo "<td>" . $row['recipe_fat'] . "</td>";
  echo "<td>" . $row['recipe_cholestrol'] . "</td>";
  echo "<td>" . $row['recipe_carbs'] . "</td>";
  echo "<td>" . $row['recipe_protein'] . "</td>";
  echo "<td>" . $row['recipe_fibre'] . "</td>";
  echo "<td>" . $row['recipe_sodium'] . "</td>";
  echo "<td>" . $row['recipe_potassium'] . "</td>";
  echo "<td><a href=\"" . $row['recipe_source'] . "\">Click here to view the recipe</a></td>";
  echo "</tr>";
}
echo "</table>";

$recipe_name = $row['recipe_name'];

echo $recipe_name;
因此,对于上面的SQL查询,我希望使用“$recipe\u name”,而不是使用配方的实际名称(这确实有效,但我希望能够将此代码用于其他配方,而不仅仅是一个配方)

谢谢你事先的帮助

Sarah

在两页上都使用session_start()

要在会话中保存$recepie,请使用

$_SESSION['recepie'] = $recepie;
在use想要使用$recepie的页面上 使用session_start()启动会话,并使用

if(isset($_SESSION['recepie']))
$recepie = $_SESSION['recepie']);

您是否使用了save_recipe页面上的session_start(),谢谢您提供了未定义的_变量。我在显示表格的页面中添加了session_start(),并对其进行了更改:$recipe_name=$row['recipe_name']$_会话['recipe\u name']=$recipe\u name;echo$recipe_name;你能告诉哪个页面先执行top还是below top页面先执行top页面你是否添加了session_start()和$_session['recipe_name']=$recipe_name;在那一页上
if(isset($_SESSION['recepie']))
$recepie = $_SESSION['recepie']);