Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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_Session_Web_Process - Fatal编程技术网

Php 为什么我的测验没有与mysql数据库交互?

Php 为什么我的测验没有与mysql数据库交互?,php,mysql,session,web,process,Php,Mysql,Session,Web,Process,我一直在编写一个基于网络的测验,我已经在这个问题上坚持了好几个星期了 基本上我使用了var_dump函数,我发现实际上我的数组是空的 C:\wamp64\www\MyProject\quick app\final.php:20: 数组大小=0 空的 问题是,我的测验可以使用mysql数据库提取问题和选项,但它无法验证输入。这就是为什么我不能同时使用score变量。如何解决此问题,以便我的测验可以使用我的数据库验证答案 您的帮助将非常感谢,并将帮助像我这样的初学者程序员。非常感谢 #Questio

我一直在编写一个基于网络的测验,我已经在这个问题上坚持了好几个星期了

基本上我使用了var_dump函数,我发现实际上我的数组是空的

C:\wamp64\www\MyProject\quick app\final.php:20: 数组大小=0 空的

问题是,我的测验可以使用mysql数据库提取问题和选项,但它无法验证输入。这就是为什么我不能同时使用score变量。如何解决此问题,以便我的测验可以使用我的数据库验证答案

您的帮助将非常感谢,并将帮助像我这样的初学者程序员。非常感谢

#Question.php

    <?php session_start(); ?>
    <?php include "database.php"; 
    ?>


    <?php
    //Set question
    $number = (int) $_GET['n'];


        /*
     * Get Total Questions
    */
     $query="SELECT * FROM questions" ;
     //Get Results
    $result = mysqli_query ($conn,$query);
    $total = mysqli_num_rows($result);



    /*
    * Get the Question
    */
    $query = "SELECT * FROM questions
    WHERE question_number = $number";

    //Get result
    $result = mysqli_query ($conn,$query);

    $question = mysqli_fetch_assoc($result);


    /*
    * Get choices
    */
    $query2 = "SELECT * FROM choices
    WHERE question_number = $number";

    //Get result
    $choices = mysqli_query($conn,$query2);


    ?>



     <!DOCTYPE html PUBLIC'>
    <html> 
        <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
            <title>
                General Knowledge Quiz
            </title>
            <link href="css/other.css" type="text/css" rel="stylesheet" />
        </head>
        <body>
        <main>


    <div class="container" >
    <div class="current">Question <?php echo $question ['question_number'];?> of <?php echo $total; ?> </div>
    <p class="question">
     <?php echo $question ['text']; ?>
    </p>
    <form action="process.php" method="post"
      <ul class="choices"

    <?php while ($row = $choices->fetch_assoc()) : ?>
    <li><input name="choice" type="radio" value="<?php echo $row ['id'];?> " /><?php echo $row ['text']; ?></li>
          <input type="submit" name="submit" value="Sumbit"/>
          <input type="hidden" name= "number" value="<?php echo $number; ?>" />



     </main>
     </div>
        </body>
    </html>

    <?php endwhile?>


在没有看到数据库模式的情况下,根据我从上面的推断,这里有一定数量的猜测,但我希望这可能有助于找到解决方案。看起来,最初使用的3个查询可以替换为连接两个表的单个查询,还可以执行子查询以查找总记录计数。类似的组合查询可以在process.php中完成

问题是。。。。Question.php

和处理请求:process.php


应该注意的是,这些都没有经过测试,因此很可能存在语法错误

您的循环在main、div、body和htmlAlso的结束标记周围不正确地循环,输入元素不是ul element的有效直接子元素您没有结束形式标记,并且开始形式标记不正确,因为没有结束>。ul标签也是如此-它缺少关闭>您错误地关闭标签-它们需要以与打开相反的顺序关闭,以便。。。应该完成…您没有结束表单标签,也没有结束ul标签哇,非常感谢!!你帮我解决了我花了这么长时间才解决的问题。谢谢你!一个小问题。我如何让它打印问题编号?它认为问题是0中的0?这篇文章应该是这样写的。。检查PHP错误日志,看看是否有任何与此相关的错误。我认为PHP错误日志中没有任何与此相关的错误。您认为问题可能是%d参数吗?我应该用别的东西吗?我不确定我发现的另一个问题是,实际问题没有打印出来。我不知道如何解决这个问题。
#Process.php

    <?php session_start(); ?>
    <?php include "database.php";?>
    <?php
    // check to see if score is set error_handler)
    if(!isset($_SESSION['score'] )) {
        $_SESSION ['score'] = 0;
    }   

    if($_POST) {
        $number = $_POST['number']; 
        $selected_choice = $_POST['choice'];
        $next = $number++;

        echo $number ;
        echo $selected_choice ; 
    }

        /*
     * Get Total Questions
    */
     $query="SELECT * FROM questions" ;
     //Get Results
    $results = mysqli_query ($conn,$query);
    $total = mysqli_num_rows($results);



        /*
        *   Get correct choice
        */
        $query = "SELECT * FROM choices
     WHERE question_number = $number AND is_correct=1";

    //Get result
    $result = mysqli_query($conn,$query);

    //Get Row
    $row = $result->fetch_assoc(); 

    //Set Correct choice
    $correct_choice = $row['id'];


    //Compare
    if ($correct_choice == $selected_choice) {
        //Answer is correct
    $_SESSION['score']++;}



    //Check if it is the last question
    if($number == $total){
        header("Location: final.php") ;
        exit();
        }  
        else { 
          header("Location: question.php?n=".$next);

        }
        ```

<?php

    session_start();
    if( !isset( $_SESSION['score'] ) )$_SESSION['score']=0;
    $number = isset( $_GET['n'] ) && is_numeric( $_GET['n'] ) ? intval( $_GET['n'] ) : 1;


    include "database.php"; 



    $sql="select
            ( select count(*) from `questions` ) as `total`,
            q.`question_number` as `qid`, 
            q.`text` as `question`, 
            c.`id` as `aid`, 
            c.`text` as `answer` 
        from `questions` q
            join `choices` c on c.`question_number`=q.`question_number`
        where q.`question_number` = ?";
    $stmt=$conn->prepare( $sql );

    if( $stmt ){
        $stmt->bind_param('i',$number);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result( $total, $qid, $question, $aid, $answer );
    }
?>
<!DOCTYPE html PUBLIC'>
<html> 
    <head>
        <meta charset="utf-8" />
        <title>General Knowledge Quiz</title>
        <link href="css/other.css" rel="stylesheet" />
    </head>
    <body>
        <main>
            <div class="container" >

                <div class="current">Question <?php printf('%d of %d',$qid,$total); ?></div>
                <p class="question"><?php echo $question; ?></p>

                <form action="process.php" method="post">
                    <ul class="choices">
                    <?php
                        if( $stmt ){
                            while( $rs=$stmt->fetch() ){
                                printf('
                                    <li>
                                        <input type="radio" name="choice" value="%d" />%s
                                    </li>',
                                    $aid,
                                    $answer
                                );
                            }
                        }
                    ?>
                    </ul>
                    <input type="submit" name="submit" value="Sumbit" />
                    <input type="hidden" name="number" value="<?php echo $number; ?>" />
                </form>
            </div>
        </main>
    </body>
</html>
<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        if( isset( $_POST['number'], $_POST['choice'] ) && is_numeric( $_POST['number'] ) ){

            session_start();
            include "database.php";

            if( !isset( $_SESSION['score'] ) ) $_SESSION['score'] = 0;
            $choice=filter_input( INPUT_POST, 'choice', FILTER_SANITIZE_NUMBER_INT );
            $number=filter_input( INPUT_POST, 'number', FILTER_SANITIZE_NUMBER_INT );


            $sql='select 
                ( select count(*) from questions ) as `total`,
                `id`
                from `choices`
                where `question_number`=? and is_correct=1;';
            $stmt=$conn->prepare( $sql );
            if( $stmt ){
                $stmt->bind_param('i',$number);
                $res=$stmt->execute();
                if( $res ){
                    $stmt->store_result();
                    $stmt->bind_result($total,$id);

                    $stmt->fetch();
                    if( $id==$choice )$_SESSION['score']++;
                    $stmt->free_result();
                    $stmt->close();


                    if( $number==$total )exit( header('Location: final.php') );
                    else exit( header( sprintf('Location: question.php?n=%s',( $number + 1 ) ) ) );
                }
            }
        }
        exit();
    }
    http_response_code(400);
?>