0

您好我正在使用此代码生成随机问题:

<?php
session_start(); //this was missing from this file
//connect to db
require_once('db_conn.php');

$id=rand(0,13); //2nd number = highest ID

if ($res = $mysqli->query("SELECT quest FROM table WHERE ID='$id'")){

   $resQ = $res->fetch_assoc(); 
   echo "" . $resQ['quest'] . "";

   $_SESSION['chckanswer'] = $resQ['guest'];

   $res->close();
}
$mysqli->close();
?>

这个尝试检查给出的答案是否与问题匹配:

<?php
session_start();

//more code

if($stmt = $mysqli->prepare('SELECT quest FROM table WHERE answer=?')){
    $stmt->bind_param("s", $_POST['given_answer']);
    $stmt->execute();
    $stmt->bind_result($quest);
    $stmt->fetch();

        if($_SESSION['chckanswer'] != $quest && !empty($_SESSION['chckanswer'])){
        echo 'wrong';
        exit();
        }
        else{
        echo 'ok';
        }
    $stmt->close();    
}
//more code

但我得到错误未定义索引 chckanswer

4

2 回答 2

0
$id=rand(0,13); // 0-13

我认为没有 id 为“零”的行

isset($_SESSION['chckanswer']) ? $_SESSION['chckanswer'] : null;

添加这一行

于 2012-07-17T13:29:52.547 回答
0

第一个文件中缺少此内容:

session_start();
于 2012-07-24T13:27:35.130 回答