<?php
session_start();
include 'connection.php';
$user_answer = $_POST['answ'];
$random = rand(1, 20);
if(isset($_POST['btn']))
{if ($user_answer == $_SESSION['answer']) {
echo '<br>RIGHT ANSWER<br>';
} else {
echo '<br>WRONG, THE RIGHT ANSWER IS ' . $_SESSION['answer'] . '<br>';
}
}
$sql = "SELECT Questions,Answers FROM history WHERE questions_id=$random";
$sth = $conn->query($sql);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
//print_r($result);
echo 'QUESTION: ' . $result['Questions'];
$_SESSION['answer']=$result['Answers'];
?>
您好。我是这个php pdo的新手。我想创建一个查询来获取问题并检查用户输入的答案。但我也不能用各种代码来做这件事。它总是提取下一个问题和答案。 SO给出了错误的答案输出。是否有任何方法可以继续会话,导致其重定向到我的主页
答案 0 :(得分:0)
我误解了这个问题: 请试试这个:
if(isset($_POST['btn']))
{
if (trim($user_answer) == trim($_SESSION['answer'])) {
echo '<br>RIGHT ANSWER<br>';
} else {
echo '<br>WRONG, THE RIGHT ANSWER IS ' . $_SESSION['answer'] . '<br>';
}
unset($_SESSION['answer']);
}
我取消了会话变量。请试试这个。