0

我有一个页面,用户在其中选择具有唯一 ID 的食谱,然后将他们发送到另一个页面,在该页面中显示该食谱的信息,但没有显示任何内容。

$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.")
    ");

这是主页上的查询,它允许用户选择一组成分,然后从中检索食谱。

echo '<form action="recipe.php?id=<?php echo $recipe_id;?>" method="POST">';
        echo '<input id="search" name="Look" type="Submit" value="Look" >';
        echo '</form>';

在每个配方下都有一个用户按下的按钮,这会将用户发送到特定的配方。

$sql = mysql_query("SELECT `name`, `image`
FROM `recipe` 
WHERE `id` = ".$_GET['id']." LIMIT 1");

这是我在结果页面上运行的查询 (recipe.php)

在页面的下方,我运行了这个。

<?php
echo $_GET['id'];
?>

它没有显示 ID,在 url 中我显示了这个:

“recipe.php?id=”

表中有数据,我做错了什么?

4

3 回答 3

2

以下回显语法不正确:

echo '<form action="recipe.php?id=<?php echo $recipe_id;?>" method="POST">';

应该:

echo '<form action="recipe.php?id='.$recipe_id.'" method="POST">';

我看不到您在哪里设置$recipe_id,这似乎是您问题的根源。

目前尚不清楚您要从哪里检索难以捉摸$recipe_id的东西。

如果您尝试从中提取它,$recipes则应将其包含在您的 sql 语句中:

SELECT `id`,....

然后在你之后把它拉进去$recipes->fetch_assoc()

$recipe_id = $recipes['id'];
于 2013-05-09T18:17:16.690 回答
1

首先,在对数据库进行任何查询之前,请确保您在查询中使用的所有 $variables 都已转义,因为您很容易被黑客入侵。像这样逃跑:

$_GET['variable'] = mysql_real_escape_string($_GET['variable']);

整数可以这样转义

$_GET['variable'] = (int) $_GET['variable'];

您做错了,您可以将 recipe_id 添加为隐藏输入字段,然后在提交表单后阅读。这是示例表格。

<form action="/recipe.php" method="POST">
    <input type="hidden" name="recipe_id" value="<?= $recipe_id ?>" />
    <input id="search" name="Look" type="Submit" value="Look" />
</form>

提交表单后,您可以执行以下操作:

$recipe_id = (int) $_POST['recipe_id'];
$sql = mysql_query("SELECT `name`, `image` FROM `recipe` WHERE `id` = $recipe_id LIMIT 1");

但我认为你的主要问题是表单中的 $recipe_id 只是一个空变量,请先检查:)。

于 2013-05-09T18:20:59.000 回答
-1
// change this
echo '<form action="recipe.php?id=<?php echo $recipe_id;?>" method="POST">';
// to this
echo '<form action="recipe.php?id=' . $recipe_id .'" method="POST">';

将您在其中回显表单的行更改为类似这样的内容...您之前尝试过两次回显(在回声中回显)...但这有效。

于 2013-05-09T18:11:04.063 回答