你好我使用這個代碼來生成隨機問題:檢查給出答案的答案,如果匹配隨機任務
<?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
但我得到的錯誤undifined指數chckanswer
你打電話的session_start();在你得到錯誤的文件中? – 2012-07-17 13:14:44
@DanPrince,該死的我1秒前。 – 2012-07-17 13:14:55
對於Mike的愛,請使用ORDER BY RAND()LIMIT 1而不是$ id = rand(0,13);如果你添加一個問題,你必須改變你的硬編碼爲13到14! – Waygood 2012-07-17 13:16:54