Php 使用会话id';让我们抓取数据

Php 使用会话id';让我们抓取数据,php,Php,我已经在一个项目上工作了几周,用户选择了一组配料,然后它会取出包含这些配料的配方,但现在我有了一个显示配方的页面,我希望用户能够单击特定配方,并被引导到一个显示该配方及其说明的页面 <?php if (isset($_POST['search'])) { $ingredient1 = mysql_real_escape_String ($_POST['dropdown1']); $ingredient2 = mysql_real_escape_String ($_POST[

我已经在一个项目上工作了几周,用户选择了一组配料,然后它会取出包含这些配料的配方,但现在我有了一个显示配方的页面,我希望用户能够单击特定配方,并被引导到一个显示该配方及其说明的页面

<?php
if (isset($_POST['search'])) {
    $ingredient1 = mysql_real_escape_String ($_POST['dropdown1']);
    $ingredient2 = mysql_real_escape_String ($_POST['dropdown2']);
    $ingredient3 = mysql_real_escape_String ($_POST['dropdown3']);


    $recipes = mysql_query("
        SELECT DISTINCT `name`, `step1`, `step2`, `step3`, `image`, `reference`
        FROM `recipe` r
        INNER JOIN `recipe_ingredients` ri
        ON r.id = ri.recipe_id
        WHERE ri.ingredient_id IN (".$ingredient1.",".$ingredient2.",".$ingredient3.")
    ");

    while ($recipe = mysql_fetch_assoc($recipes)) {

        echo '<section id="row">';
        echo '<br />';
        echo $recipe['name'].'<br />'; 
        echo '<br />';
        echo "<img src=\"{$recipe['image']}\" height=100 width=100 /><br />";
        echo '<form action="recipe.php">';
        echo '<input id="search" name="Look" type="Submit" value="Look" >';
        echo '</form>';
        echo '<br />';
        echo 'You will Need: ';
        echo '<br />';
        echo '</section>';

        //echo 'Step 1: ';
        //echo $recipe['step1'].'<br />';
        //echo '<br />';
        //echo 'Step 2: ';
        //echo $recipe['step2'].'<br />';
        //echo '<br />';
        //echo 'Step 3: ';
        //echo $recipe['step3'].'<br />';
        //echo '<br />';
        //echo '<br />';
        //echo '<a href="http://'.$recipe['reference'].'">'.$recipe['reference'].'</a>';  
        //echo '</li>';
        //echo '</ul>';


    }
}


?>
我需要使用会话id吗

这将显示结果和必须按下才能进入“配方说明”页面的按钮。

这是指向您的页面,它将显示配方图像、名称、成分和说明

<?php
if (isset($_POST['search'])) {
    $ingredient1 = mysql_real_escape_String ($_POST['dropdown1']);
    $ingredient2 = mysql_real_escape_String ($_POST['dropdown2']);
    $ingredient3 = mysql_real_escape_String ($_POST['dropdown3']);


    $recipes = mysql_query("
        SELECT DISTINCT `name`, `step1`, `step2`, `step3`, `image`, `reference`
        FROM `recipe` r
        INNER JOIN `recipe_ingredients` ri
        ON r.id = ri.recipe_id
        WHERE ri.ingredient_id IN (".$ingredient1.",".$ingredient2.",".$ingredient3.")
    ");

    while ($recipe = mysql_fetch_assoc($recipes)) {

        echo '<section id="row">';
        echo '<br />';
        echo $recipe['name'].'<br />'; 
        echo '<br />';
        echo "<img src=\"{$recipe['image']}\" height=100 width=100 /><br />";
        echo '<form action="recipe.php">';
        echo '<input id="search" name="Look" type="Submit" value="Look" >';
        echo '</form>';
        echo '<br />';
        echo 'You will Need: ';
        echo '<br />';
        echo '</section>';

        //echo 'Step 1: ';
        //echo $recipe['step1'].'<br />';
        //echo '<br />';
        //echo 'Step 2: ';
        //echo $recipe['step2'].'<br />';
        //echo '<br />';
        //echo 'Step 3: ';
        //echo $recipe['step3'].'<br />';
        //echo '<br />';
        //echo '<br />';
        //echo '<a href="http://'.$recipe['reference'].'">'.$recipe['reference'].'</a>';  
        //echo '</li>';
        //echo '</ul>';


    }
}


?>



这是从数据库中获取信息的php。

我假设您的数据库中所有食谱都有id号。您可以使用
$\u GET
变量,使每个“look”按钮都是一个包含以下内容的链接:

<?php

//Link part of message
echo "<a href='/recipies/detail.php?id=" . $recipe['id'] . "'>" . $recipe['name'] . "</a>";

?>


这样,用户就可以轻松地将配方页面添加到书签中或与他人共享该页面。然后在
details.php
页面上,您只需在数据库中查找具有指定id的配方。

使用配方id,而不是会话id-

首先将
id
添加到查询中

// add your `recipe`.`id` to your search query
SELECT DISTINCT `id`, `name`, `step1`, `step2`, `step3`, `image`, `reference`
    FROM `recipe` r
    INNER JOIN `recipe_ingredients` ri
    ON r.id = ri.recipe_id
    WHERE ri.ingredient_id IN (".$ingredient1.",".$ingredient2.",".$ingredient3.")
   $recipe_id = mysql_real_escape_string ($_GET['id']);
然后将该id放入表单操作链接中-

    echo "<form action=\"recipe.php?id={$recipe['id']}\">";

您的实际问题是“我需要使用会话id吗?”@sircapsalot我想知道这是否是实现这一点的最佳方法。从表中获取配方
id
,以及步骤,或者将
Look
按钮作为链接(例如,指向
recipe.php?id=
)或者保留一个表单并添加一个隐藏字段:
谢谢,我的食谱确实有一个ID,我会尝试一下。我还是个新手,所以我会更多地研究$\u获取变量。我很高兴这个解决方案适合您。确保选择对您最有帮助的答案(通过单击答案排名下方的复选框)。