2012-07-17 110 views
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 

但我得到的錯誤undifined指數chckanswer

+1

你打電話的session_start();在你得到錯誤的文件中? – 2012-07-17 13:14:44

+0

@DanPrince,該死的我1秒前。 – 2012-07-17 13:14:55

+1

對於Mike的愛,請使用ORDER BY RAND()LIMIT 1而不是$ id = rand(0,13);如果你添加一個問題,你必須改變你的硬編碼爲13到14! – Waygood 2012-07-17 13:16:54

回答

0

這是從1號文件丟失:

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

I D on't覺得沒有一行ID爲 「零」

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

加入這一行

+0

但這並不能解決問題 – Pavlos1316 2012-07-17 13:47:23