Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法在mysql中随机化问题行_Php_Mysql - Fatal编程技术网

Php 无法在mysql中随机化问题行

Php 无法在mysql中随机化问题行,php,mysql,Php,Mysql,我试图从我的sql表中获取随机问题。rand()函数不起作用。“answers”的rand()函数正在工作,但“questions”行不起作用。我哪里做错了。有人能给出解决办法吗 index.php <?php $msg = ""; if(isset($_GET['msg'])){ $msg = $_GET['msg']; $msg = strip_tags($msg); $msg = addslashes($msg);

我试图从我的sql表中获取随机问题。rand()函数不起作用。“answers”的rand()函数正在工作,但“questions”行不起作用。我哪里做错了。有人能给出解决办法吗

index.php

<?php 

    $msg = "";
    if(isset($_GET['msg'])){
        $msg = $_GET['msg'];
        $msg = strip_tags($msg);
        $msg = addslashes($msg);
    }
    ?>
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Quiz Tut</title>
    <script>
    function startQuiz(url){
        window.location = url;
    }
    </script>
    </head>
    <body>
    <?php echo $msg; ?>
    <h3>Click below when you are ready to start the quiz</h3>
    <button onClick="startQuiz('quiz.php?question=1')">Click Here To Begin</button>
    </body>
    </html>

图坦卡蒙
函数startQuiz(url){
window.location=url;
}
准备好开始测验时,请单击下面的
单击此处开始
questions.php

<?php 

session_start();
require_once("scripts/connect_db.php");
$arrCount = "";
if(isset($_GET['question'])){
    $question = preg_replace('/[^0-9]/', "", $_GET['question']);
    $output = "";
    $answers = "";
    $q = "";
    $sql = mysql_query("SELECT id FROM questions");
    $numQuestions = mysql_num_rows($sql);
    if(!isset($_SESSION['answer_array']) || $_SESSION['answer_array'] < 1){
        $currQuestion = "1";
    }else{
        $arrCount = count($_SESSION['answer_array']);
    }
    if($arrCount > $numQuestions){
        unset($_SESSION['answer_array']);
        header("location: index.php");
        exit();
    }
    if($arrCount >= $numQuestions){
        echo 'finished|<p>There are no more questions. Please enter your first and last name and click next</p>
                <form action="userAnswers.php" method="post">
                <input type="hidden" name="complete" value="true">
                <input type="text" name="username">
                <input type="text" name="email">
                <input type="submit" value="Finish">
                </form>';
        exit();
    }
    $singleSQL = mysql_query("SELECT * FROM questions WHERE id='$question' order by RAND() LIMIT 1");

        while($row = mysql_fetch_array($singleSQL)){
            $id = $row['id'];
            $thisQuestion = $row['question'];
            $type = $row['type'];
            $question_id = $row['question_id'];
            $q = '<h2>'.$thisQuestion.'</h2>';
            $sql2 = mysql_query("SELECT * FROM answers WHERE question_id='$question' ORDER BY rand()");
            while($row2 = mysql_fetch_array($sql2)){
                $answer = $row2['answer'];
                $correct = $row2['correct'];
                $answers .= '<label style="cursor:pointer;"><input type="checkbox" name="rads" value="'.$correct.'">'.$answer.'</label> 
                <input type="hidden" id="qid" value="'.$id.'" name="qid"><br /><br />
                ';

            }
            $output = ''.$q.','.$answers.',<span id="btnSpan"><button onclick="post_answer()">Submit</button></span>';
            echo $output;
           }
        }


?>

测验页
功能倒计时(秒、元素){
var元素=document.getElementById(elem);
element.innerHTML=“您还有“+秒+”秒。”;
如果(秒<1){
var xhr=new XMLHttpRequest();
var url=“userAnswers.php”;
var vars=“checkbox=0”+”和qid=“+;
xhr.open(“POST”,url,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xhr.onreadystatechange=函数(){
如果(xhr.readyState==4&&xhr.status==200){
警告(“您没有在分配的时间内回答问题。它将被标记为不正确。”);
清除超时(计时器);
}
}
xhr.send(vars);
document.getElementById('counter_status')。innerHTML=“”;
document.getElementById('btnSpan')。innerHTML='Times Up!';
document.getElementById('btnSpan')。innerHTML+='';
}
秒--;
var timer=setTimeout('倒计时('+secs+',“'+elem+”),1000);
}
函数getQuestion(){
var hr=新的XMLHttpRequest();
hr.onreadystatechange=函数(){
如果(hr.readyState==4&&hr.status==200){
var response=hr.responseText.split(“|”);
如果(响应[0]=“已完成”){
document.getElementById('status').innerHTML=response[1];
}
var nums=hr.responseText.split(“,”);
document.getElementById('question').innerHTML=nums[0];
document.getElementById('answers').innerHTML=nums[1];
document.getElementById('answers').innerHTML+=nums[2];
}
}
hr.open(“GET”,“questions.php?question=“+,true”);
hr.send();
}
函数x(){
var rads=document.getElementsByName(“rads”);
对于(变量i=0;i
似乎您一次只选择一个问题,所以没有什么可以随机排序的

如果要选择一个随机问题,查询可能如下所示:

SELECT * FROM questions WHERE 1=1 order by RAND() LIMIT 1

。。。但这需要重写一些相关的代码:例如,您不需要每次都强制执行
question
get参数。

非常感谢它的工作。但现在答案选项并没有显示与他们的特定问题相关的内容。如何正确显示?使用随机问题的id选择答案:
“从答案中选择*,其中问题id=“$question\u id”按兰德顺序()”
问题和答案是循环的。测验不会进入“用户答案”页面,就像永远不会结束一样。请帮忙编码。我是php新手。我认为arrCount没有正确增加。
SELECT * FROM questions WHERE id='$question'
SELECT * FROM questions WHERE 1=1 order by RAND() LIMIT 1