将测验结果保存到数据库

时间:2019-05-03 15:30:57

标签: php html sql pdo

我创建了一个多项选择测验/问卷。问题和答案是从数据库中提取的,并且我在网页上设置了会话。在此刻,用户只能在获得结果时打印该网页。

我正在使用phpmyadmin和php进行测验。我目前正在从一个名为问题的数据库表中提取问题,这些字段包括questionID,question,ansYes和ansNo。

<?
//Always start this first
session_start();
include ("dbConnect.php");

if ( isset( $_SESSION['user_email'] ) ) {
     //Grab user data from the database using user email

} else {
   // Redirect them to the login page
    header("login.php");
}
$sql = "SELECT * FROM questions";
$dbQuery = $db->prepare($sql);
$dbQuery->execute();
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>List</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

  <style>


/* CSS animation */

@-webkit-keyframes fadeIn {
    0% { 
        opacity:0; 
        transform: scale(0.6);
    }

    100% {
        opacity:100%;
        transform: scale(1);
    }
}

@keyframes fadeIn {
    0% { opacity:0; }
    100% { opacity:100%; }
}  
  </style>

 <?php include("navIn.php"); ?>


<body>

<div class="container-fluid">
    <br>

<?php

    /*Printing out questions*/

    if(!isset($_POST["submitForm"])) {
        echo "<form method='post' action='checklist.php'>";

       if (isset($_SESSION["first_name"])) {
            echo '<span style="color:#000000;position:left; left:1060px;">Hello '.$_SESSION["first_name"].'!<br></span>';
       }
        while($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {


           echo"<div class=container-fluid text-center>";
            echo "<h4>" . $dbRow["question"] . "</h4>";
            echo "<h4><input type='radio' required name='" . $dbRow["questionID"] . "' value='Yes'> Yes <br></h4?";
            echo "<h4><input type='radio' required name='" . $dbRow["questionID"] . "' value='No'> No <br><br></h4>";
            echo "</div>";

        }
        echo "<input type='submit' name='submitForm' value='Submit'>";
        echo "</form>";

        echo"</div>";


    } else {

        if (isset($_SESSION["first_name"])) {
            echo '<span style="color:#000000;position:left; left:1060px;">Hello '.$_SESSION["first_name"].'!<br></span>';
       }
        /*printing out results from questions */
        echo"<div class=container-fluid text-center>";
        while($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
            if($_POST[$dbRow["questionID"]] == "Yes") {
                 echo '<h4 span style="color:blue;"> '.$dbRow["question"].'</span>';
                /*echo "<h4>" .$dbRow["question"]."<br></h4><br>";*/
                echo "<h4>" . $dbRow["ansYes"] . "</h4><br>";
            } else {
                echo '<h4 span style="color:blue;"> '.$dbRow["question"].'</span>';
         /*       echo "<h4>" .$dbRow["question"]."<br></h4><br>";*/
                echo "<h4>" . $dbRow["ansNo"] . "</h4><br>";


            }


        }
          echo"Click the button bellow to print this page!";
        echo'<br><form> <input type=button value="Print me!" onClick="javascript:window.print()"> </form>';
    echo"</div>";
    }

        ?>

</div>
</body>
 <?php include ("footer.php"); ?>
</html>

我希望能够保存用户结果,以便他们每次访问checklist.php时都可以得到结果,而不必将页面打印出来。

0 个答案:

没有答案