我创建了一个数组并将其存储在$ _SESSION变量中,现在我想在MySQL select语句中包含该数组以获取更多信息,这取决于p,它包含在我的URL中。我的代码包含在HTML页面中,除了必要的HTML以及session_start之外,它完全是空的,所以这不是问题所在。代码看起来像这样:
<?php
$p = $_GET["p"];
var_dump($_SESSION['questions'][$p]);
include("../script/db_connect.php");
$p = $_GET["p"];
$select_right_question = "select * from questions where id = '{$_SESSION['questions'][$p]}'";
$question_infos = mysqli_query($con, $select_right_question);
while ($row = mysqli_fetch_assoc($question_infos)) {
echo $row["question"] . '</br>'
. $row["right_answer"] . '</br>'
. $row["answer2"] . '</br>'
. $row["answer3"] . '</br>'
. $row["answer4"];
}
mysqli_close($con);
?>
我已经测试了它,主要问题是以下几行:
$select_right_question = "select * from questions where id = '{$_SESSION['questions'][$p]}'";
在此处包含SESSION变量的正确方法是什么?
答案 0 :(得分:0)
忽略代码的安全问题,并假设$_SESSION['questions'][$p]
返回一个数组,您可以将SQL语句修改为:
$select_right_question = "select * from questions where id = '{$_SESSION['questions'][$p]['id']}'";