Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>MarkQuiz</title>
- <link rel="stylesheet" type="text/css" href="styles/style.css">
- </head>
- <body>
- <?php
- require_once ("settings.php"); // Connection info
- $conn = @mysqli_connect($host,
- $user,
- $pwd,
- $sql_db
- );
- ////////////////////////////////////////////////////////////
- function sanitise_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- $errMsg = "";
- ////////////////////////////////////////////////////////////
- if(isset ($_POST["firstname"])) {
- $firstname = $_POST["firstname"];
- $firstname = sanitise_input($firstname);
- if($firstname = "") {
- $errMsg .= "<p>you must enter your first name.</p>";
- }
- elseif (!preg_match("/^[a-zA-Z]*$/", $firstname)) {
- $errMsg .= "<p>Only alpha characters allowed</p>";
- }
- }
- if(isset ($_POST["lastname"])) {
- $lastname = $_POST["lastname"];
- $lastname = sanitise_input($lastname);
- if($lastname = "") {
- $errMsg .= "<p>you must enter your last name</p>";
- }
- elseif (!preg_match("/^[a-zA-Z]*$/", $lastname)) {
- $errMsg .= "<p>Only alpha characters allowed</p>";
- }
- }
- if(isset ($_POST["studentnumber"])) {
- $studentid = $_POST["studentnumber"];
- $studentid = sanitise_input($studentid);
- if($studentid = "") {
- $errMsg .= "<p>you must enter your last name</p>";
- }
- elseif (!preg_match("/^[0-9]{7}$|^[0-9]{10}$/", $studentid)) {
- $errMsg .= "<p>Your Student ID needs to be between 7-10</p>";
- }
- }
- if ($errMsg != ""){
- echo "<p>$errMsg</p>";
- }
- ///////////////////////////////////////////////////////////
- ///Create table with no attempt counter
- $query = "CREATE TABLE IF NOT EXISTS attempts (
- attempt_id INT AUTO_INCREMENT PRIMARY KEY,
- studentid INT,
- firstname VARCHAR(30) NOT NULL,
- lastname VARCHAR(30) NOT NULL ,
- score VARCHAR(5) NOT NULL
- );";
- $result = mysqli_query($conn, $query);
- ////////////////////////////////////////////////////////////
- //Check if attempts is < 3
- $attemptcount = 0;
- if (isset($_POST["studentnumber"])) {
- $studentid = $_POST["studentnumber"];
- }
- $query = "SELECT COUNT(attempt_id) AS attemptcounter FROM $sql_table WHERE studentid = '$studentid';";
- if ($result = mysqli_query($conn, $query)) {
- $attemptRow = mysqli_fetch_accoc($result)
- $attemptCount = $attempt['attemptcounter']
- }
- mysqli_close($conn);
- ///////////////////////////////////////////////////////////
- $score = 0;
- if (isset ($_POST["question1"])) {
- $question1 = $_POST["question1"];
- $question1 = sanitise_input($question1);
- }
- if (isset ($_POST["question2"])) {
- $question2 = $_POST["question2"];
- }
- if (isset ($_POST["question3"])) {
- $question3 = $_POST["question3"];
- }
- if (isset ($_POST["question4"])) {
- $question4 = $_POST["question4"];
- }
- if (isset ($_POST["question5"])) {
- $question5 = $_POST["question5"];
- }
- if ($question1 == "facebook") {
- $score = $score + 20;
- }
- if ($question2 == "python") {
- $score = $score + 20;
- }
- if ($question3 == "singlepage"){
- $score = $score + 20;
- }
- if ($question4 == "search") {
- $score = $score + 20;
- }
- if ($question5 == "one") {
- $score = $score + 20;
- }
- ///////////////////////////////////////////////////////////
- // Checks if connection is succesful
- if (!$conn) {
- // Displays an error message
- echo "<p>Database connection failure</p>"; //not in production script
- } else {
- //upon succesful connection
- $sql_table="attempts";
- $studentid = trim($_POST["studentnumber"]);
- $firstname = trim($_POST["firstname"]);
- $lastname = trim($_POST["lastname"]);
- $query = "insert into $sql_table (studentid, firstname, lastname, score) values ('$studentid', '$firstname', '$lastname', $score)";
- $result = mysqli_query($conn, $query);
- echo "<p>result :", $result, "</p>";
- if(!$result) {
- echo "<p class=\"wrong\">something is wrong with ", $query, "</p>";
- } else {
- echo "<p class\"ok\">Succesfully added new record</p>";
- }
- mysqli_close($conn);
- }
- ///////////////////////////////////////////////////////////
- require_once ("settings.php"); // Connection info
- $conn = @mysqli_connect($host,
- $user,
- $pwd,
- $sql_db
- );
- // Checks if connection is succesful
- if (!$conn) {
- // Displays an error message
- echo "<p>Database connection failure</p>"; //not in production script
- } else {
- //upon succesful connection
- $sql_table="attempts";
- //set up the SQL command to query or add data into the table
- $query = "SELECT attempt_id, studentid, firstname, lastname, score, Count(attempt_id) AS attemptcount FROM attempts";
- //execute the query and store result into the result pointer
- $result = mysqli_query($conn, $query);
- //checks if the execution was succesful
- if(!$result) {
- echo "<p> something is wrong with", $query, "</p>";
- } else {
- //display the retrieved records
- echo "<table border=\"1\">\n";
- echo "<tr>\n "
- ."<th scope=\"col\">attempt_id</th>\n "
- ."<th scope=\"col\">studentid</th>\n "
- ."<th scope=\"col\">firstname</th>\n "
- ."<th scope=\"col\">lastname</th>\n "
- ."<th scope=\"col\">score</th>\n "
- ."<th scope=\"col\">attemptcount</th>\n "
- ."<tr>\n ";
- // retrieve current record pointed by the result pointer
- while ($row = mysqli_fetch_assoc($result)){
- echo "<tr>\n";
- echo "<td>", $row["attempt_id"], "</td>\n";
- echo "<td>", $row["studentid"], "</td>\n";
- echo "<td>", $row["firstname"], "</td>\n";
- echo "<td>", $row["lastname"], "</td>\n";
- echo "<td>", $row["score"], "</td>\n";
- echo "<td>", $row["attemptcount"], "</td>\n";
- echo "</tr>\n ";
- }
- echo "</table>\n "<!DOCTYPE html>
Add Comment
Please, Sign In to add comment