测验的结果未存储到数据库的“结果”
我尝试存储我的
$ _ session ['score']
在变量中
$score = $_session['score']
但没有任何反应 以下是我所有的测验文件
index.php (这是用户选择测验时看到的第一页。此页工作正常)
<?php include "database.php"; ?>
<?php
//Get the total questions
$query="select * from questions";
//Get Results
$results = $mysqli->query($query) or die ($mysqli->error.__LINE__);
$total = $results->num_rows;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<?php include "nav-bar.php" ?>
<title>Quizzer!</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="container">
<header>
<div class="container">
<br>
<br>
<br>
<h1>LaBoa weekly quiz</h1>
</div>
</header>
<main>
<div class="container">
<br>
<h2>Test your knowlege</h2>
<br>
<p>This is a multiple choice quize to test your knowledge about something</p>
<ul>
<li><strong>Number of Questions: </strong><?php echo $total; ?></ul>
<li><strong>Type: </strong>Multiple Choice</ul>
<li><strong>Estimated Time: </strong><?php echo $total*0.5; ?> minutes</ul>
</ul>
<a href="question.php?n=1" class="start">Start Quiz</a>
</div>
</div>
</main>
<footer>
<div class="container">
Copyright © 2019, LaBoa
</div>
</footer>
</body>
</html>
process.php (此文件是我创建$ _SESSION ['score']的位置。它汇总了用户正确输入的所有答案。
<?php include 'database.php'; ?>
<?php session_start(); ?>
<?php
//Check to see if score is set_error_handler
if (!isset($_SESSION['score'])){
$_SESSION['score'] = 0;
}
//Check if form was submitted
if($_POST){
$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next=$number+1;
$total=4;
//Get total number of questions
$query="SELECT * FROM `questions`";
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total=$results->num_rows;
//Get correct choice
$q = "select * from `choices` where question_number = $number and is_correct=1";
$result = $mysqli->query($q) or die($mysqli->error.__LINE__);
$row = $result->fetch_assoc();
$correct_choice=$row['id'];
//compare answer with result
if($correct_choice == $selected_choice){
$_SESSION['score']++;
}
if($number == $total){
$score = $_POST['score'];
header("Location: final.php");
exit();
} else {
header("Location: question.php?n=".$next."&score=".$_SESSION['score']);
}
}
?>
final.php(这里是测验结果的支付地。尽管显示了结果,但并没有保存到thr数据库中)
<?php include "database.php"; ?>
<?php session_start(); ?>
<?php
//Create Select Query
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<?php include "nav-bar.php" ?>
<title>
Quizzer!</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="container">
<header>
<div class="container">
<br>
<br>
<br>
<h1>LaBoa Weekly Quiz</h1>
</div>
</header>
<main>
<div class="container">
<h2>You are Done!</h2>
<p>Congrats! You have completed the test</p>
<p>Final socre: <?php echo $_SESSION['score']; ?></p>
<a href="question.php?n=1" class="start">Take Test Again</a>
<?php
$query="INSERT into results (account_id, score, score_date)
values('name', 'score', date)";
?>
<?php session_destroy(); ?>
</div>
</main>
<footer>
<div class="container">
Copyright © 2019, LaBoa
</div>
</footer>
</body>
</html>
这是我的测验的sql
-- Table structure for table `questions`
--
CREATE TABLE `questions` (
`question_number` int(11) NOT NULL,
`question` text COLLATE utf16_bin NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_bin;
--
-- Table structure for table `results`
--
CREATE TABLE `results` (
`result_id` int(11) NOT NULL,
`account_id` int(11) NOT NULL,
`score` int(11) NOT NULL,
`score_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NOT NULL,
`img` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
测验应该是学校项目中较大应用程序的一部分。我还将在下面链接有关该用户的文件,因为它们可能会有所帮助。
register.php
<?php
// Change this to your connection info.
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$DB_NAME = 'phplogin';
// Try and connect using the info above.
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if ($mysqli->connect_errno) {
// If there is an error with the connection, stop the script and display the error.
die ('Failed to connect to MySQL: ' . $mysqli->connect_errno);
}
// Now we check if the data was submitted, isset will check if the data exists.
if (!isset($_POST['firstname'], $_POST['lastname'], $_POST['username'], $_POST['password'], $_POST['email'])) {
// Could not get the data that should have been sent.
die ('Please complete the registration form!<br><a href="register.html">Back</a>');
}
// Make sure the submitted registration values are not empty.
if (empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) {
// One or more values are empty...
die ('Please complete the registration form!<br><a href="register.html">Back</a>');
}
// We need to check if the st_account with that username exists
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
die ('Email is not valid!<br><a href="register.html">Back</a>');
if (preg_match('/[A-Za-z0-9]+/', $_POST['username']) == 0) {
die ('Username is not valid!<br><a href="register.html">Back</a>');
}
if (strlen($_POST['password']) > 20 || strlen($_POST['password']) < 5) {
die ('Password must be between 5 and 20 characters long.<br><a href="register.html">Back</a>');
}
}
if ($stmt = $mysqli->prepare('SELECT id, password FROM users WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
// Store the result so we can check if the st_account exists in the database.
if ($stmt->num_rows > 0) {
// Username already exists
echo 'Username exists, please choose another!<br><a href="register.html">Back</a>';
} else {
// Username doesnt exists, insert new st_account
if ($stmt = $mysqli->prepare('INSERT INTO users (firstname, lastname, username, password, email) VALUES (?, ?, ?, ?, ?)')) {
// We do not want to expose passwords in our database, so hash the password and use password_verify when a user logs in.
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt->bind_param('sssss', $_POST['firstname'], $_POST['lastname'], $_POST['username'], $password, $_POST['email']);
$stmt->execute();
echo 'You have successfully registered, you can now login!<br><a href="index.html">Login</a>';
} else {
echo 'Could not prepare statement!';
}
}
$stmt->close();
} else {
echo 'Could not prepare statement!';
}
$mysqli->close();
?>
authenticate.php
<?php
session_start();
// Change this to your connection info.
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$DB_NAME = 'phplogin';
// Try and connect using the info above.
$con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if ( mysqli_connect_errno() ) {
// If there is an error with the connection, stop the script and display the error.
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// Now we check if the data was submitted, isset will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
// Could not get the data that should have been sent.
die ('Username and/or password does not exist!');
}
// Prepare our SQL
if ($stmt = $con->prepare('SELECT id, password FROM users WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
// Store the result so we can check if the st_account exists in the database.
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
// st_account exists, now we verify the password.
if (password_verify($_POST['password'], $password)) {
// Verification success! User has loggedin!
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
include_once 'homepage.php';
// echo 'Welcome ' . $_SESSION['name'] . '!';
} else {
echo 'Incorrect username and/or password!';
}
} else {
echo 'Incorrect username and/or password!';
}
$stmt->close();
} else {
echo 'Could not prepare statement!';
}
?>
home.php
<?php
// check to see if the user is logged in
if ($_SESSION['loggedin']) {
// user is logged in
include_once 'homepage.php';
} else {
// user is not logged in, send the user to the login page
header('Location: index.html');
}
?>
对不起,如果我发布的信息太多或太少,我真的很绝望,多年来一直困扰于这个问题,我真的不知道该怎么办。请不要在我仍在学习的评论中惹恼我。
final.php的代码
<?php include "database.php"; ?>
<?php session_start(); ?>
<?php
//Create Select Query
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<?php include "nav-bar.php" ?>
<title>
Quizzer!</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="container">
<header>
<div class="container">
<br>
<br>
<br>
<h1>LaBoa Weekly Quiz</h1>
</div>
</header>
<main>
<div class="container">
<h2>You are Done!</h2>
<p>Congrats! You have completed the test</p>
<p>Final socre: <?php echo $_SESSION['score']; ?></p>
<a href="question.php?n=1" class="start">Take Test Again</a>
<?php
$con->query("INSERT into results (account_id, score, score_date)
values($name, $score, now())");
?>
<?php session_destroy(); ?>
</div>
</main>
<footer>
<div class="container">
Copyright © 2019, LaBoa
</div>
</footer>
</body>
</html>
答案 0 :(得分:1)
我确定这是否有帮助,在final.php中,您有一个查询,但是我看不到查询的执行位置,并且应该对值进行硬编码吗?
$query="INSERT into results (account_id, score, score_date)
values('name', 'score', date)";
应该是这样的:
查看结果表,您需要在查询result_id
,account_id
,score
和score_date中提供以下值
更新结果表,使result_id自动增加
$accountId = $_SESSION['id'];
$score = $_SESSION['score'];
$scoreDate = date('Y-m-d H:i:s');
$con->query("INSERT into results (account_id, score, score_date)
values($accountId , $score, $scoreDate )");
答案 1 :(得分:0)
看起来您刚刚分配了变量$query
,而从未执行过它。在您的 final.php 文件中查看这一行。
<?php
$query="INSERT into results (account_id, score, score_date)
values('name', 'score', date)";
?>
<?php session_destroy(); ?>
这是PHP的最后一行,因此您从未使用$query
变量做任何事情。