使用会话ID来获取数据

时间:2013-05-07 19:09:34

标签: php

我已经在一个项目上工作了几个星期,用户选择了一组成分,然后把它们的成分拉出来,但现在我有一个显示食谱的页面,我希望用户能够点击特定食谱并被定向到显示该食谱及其说明的页面。

我是否需要使用会话ID?

显示结果和必须按下的按钮才能进入食谱说明页面。 enter image description here

这是您指向的页面,它将显示食谱图像,名称,成分和说明。

enter image description here

<?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。

2 个答案:

答案 0 :(得分:2)

我假设您的所有食谱都有数据库中的ID号。您可以使用$_GET变量,以便每个“外观”按钮都是包含以下内容的链接:

<?php

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

?>

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

答案 1 :(得分:0)

使用食谱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.")

然后将该ID放入表单操作链接 -

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

然后在recipe.php上,您可以在查询中使用id

   $recipe_id = mysql_real_escape_string ($_GET['id']);