Php echo$\u GET[';id';]不显示任何内容

Php echo$\u GET[';id';]不显示任何内容,php,mysql,Php,Mysql,我有一个页面,用户在其中选择一个具有唯一ID的配方,然后将它们发送到另一个页面,该页面显示该配方的信息,但不显示任何内容 $recipes = mysql_query(" SELECT DISTINCT `name`, `step1`, `step2`, `step3`, `image`, `reference` FROM `recipe` r INNER JOIN `recipe_ingredients` ri ON r.id =

我有一个页面,用户在其中选择一个具有唯一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>';
这是我在结果页面(recipe.php)上运行的查询

再往下一页我写了这个

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

它没有显示ID,在url中,我显示了以下内容:

“recipe.php?id=”

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

//更改此项
// 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">';

echo'以下echo语法不正确:

echo '<form action="recipe.php?id=<?php echo $recipe_id;?>" method="POST">';
然后在您的
$recipes->fetch_assoc()
之后将其拉入:


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

$_GET['variable'] = mysql_real_escape_string($_GET['variable']);
$recipe_id = (int) $_POST['recipe_id'];
$sql = mysql_query("SELECT `name`, `image` FROM `recipe` WHERE `id` = $recipe_id LIMIT 1");
整数可以这样转义

$_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\u id只是一个空变量,请首先检查:)。

您的
正在使用POST;您需要以
$\u POST['id']
的身份访问变量,或者将表单的方法更改为GETi don't seen you setting
$recipe\u id
anywhere@SamuelCook应该是别的吗?你应该在sql语句中参数化你的变量。@Lawrence:我好像记得,你没有回答。请注意回复所有帮助你的人,特别是如果你没有在这里提供答案的话。他不是在谈论
recipe.php吗?id=
part?@DannyHearnah这个问题不是100%清楚,但我想是的。。。更新了我的答案以反映这一点。我可能做错了,但你的意思是这样吗?虽然($recipe=mysql\u fetch\u assoc($recipes)){$recipe\u id=$recipes['id'];
$recipe\u id=$recipe['id'];
它现在可以工作了,但我必须使用表单中的POST方法并在recipe.php上进行查询。无论如何,谢谢XD
$_GET['variable'] = mysql_real_escape_string($_GET['variable']);
$_GET['variable'] = (int) $_GET['variable'];
<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");