英文:
Why is my PHP conditional statement not working properly?
问题
以下是您要求的代码部分的中文翻译:
PHP代码的目的是创建一个包含两个问题的数学测验。用户将回答这两个问题并单击提交按钮。
单击提交按钮后,用户的答案将与正确答案进行比较(每个问题将单独比较),然后计算总分$totalScore
。
最后,用户的数据将插入数据库,然后用户将被重定向到不同的网页。
我唯一的问题是$totalscore
的值,这个值要么是1,要么是0,无论用户是否回答正确。这是随机的。我不知道为什么。
- 如果用户回答两个问题都正确,
$totalscore
的值应该等于2 - 如果用户回答其中一个问题正确,
$totalscore
的值应该等于1 - 如果用户回答两个问题都错误,
$totalscore
的值应该等于0
但事实并非如此。请帮助
<?php
get_header();
//生成一些随机数字
$number1 = rand(1, 10); // 生成1到10之间的随机数
$number2 = rand(1, 10); // 再生成1到10之间的随机数
$number3 = rand(1, 10); // 再生成1到10之间的随机数
$number4 = rand(1, 10); // 再生成1到10之间的随机数
// 计算两个问题的答案
$answer1 = $number1 + $number2;
$answer2 = $number3 + $number4;
$totalScore = 0; // 初始化总分
?>
<form method="post">
<p>问题1:<?php echo $number1; ?> + <?php echo $number2; ?> 是多少?</p>
<input type="text" name="answerIn1" placeholder="输入您的答案" required><br>
<p>问题2:<?php echo $number3; ?> + <?php echo $number4; ?> 是多少?</p>
<input type="text" name="answerIn2" placeholder="输入您的答案" required><br>
<div class="centered-container-ForSubmitBtn">
<input type="submit" name="submit_answer" value="提交" style="border: 1px solid;">
</div>
</form>
<?php
// 检查表单是否已提交
if (isset($_POST['submit_answer'])) {
$userAnswers1 = $_POST['answerIn1']; // 从提交的表单中获取用户的答案,问题1
$userAnswers2 = $_POST['answerIn2']; // 从提交的表单中获取用户的答案,问题2
$user_name = $_SESSION['user_name']; // 从会话中检索用户名
if ($userAnswers1 == $answer1) {
$totalScore = $totalScore + 1;
}
if ($userAnswers2 == $answer2) {
$totalScore = $totalScore + 1;
}
global $wpdb; // 访问全局的 $wpdb 对象
$table_name = $wpdb->prefix . 'My_user_test_info'; // 使用WordPress前缀添加表名
// 准备并执行SQL查询以将数据插入数据库
$data = array(
'user_names' => $user_name,
'test_scores' => $totalScore
);
$wpdb->insert($table_name, $data);
// 将用户重定向到不同的网页
wp_redirect(admin_url('/results-page/'));
}
?>
<?php
get_footer();
?>
英文:
The purpose of the PHP code below is to create a math quiz consisting of two questions. The user will answer the two questions and click on the submit button.
After clicking the submit button, the user's answers will be compared with the right answers (each question will be compared separately) then the total score $totalScore
is calculated.
Finally, the user's data will be inserted into the database then the user will be directed to a different webpage.
The only problem I have is the value for $totalscore
, this value is either 1 or 0, whether the user answers all the questions right or wrong. it's random. i don't know why.
- the value for
$totalscore
should be = 2 if user answers the two questions correctly - the value for
$totalscor
e should be = 1 if user answers one of the questions correctly - the value for
$totalscore
should be = 0 if user answers the two questions incorrectly
But that is not the case. please help
<?php
get_header();
//GENERATING SOME RANDOM NUMBERS
$number1 = rand(1, 10); // Generate a random number between 1 and 10
$number2 = rand(1, 10); // Generate another random number between 1 and 10
$number3 = rand(1, 10); // Generate another random number between 1 and 10
$number4 = rand(1, 10); // Generate another random number between 1 and 10
// Calculate the answers for the two questions
$answer1 = $number1 + $number2;
$answer2 = $number3 + $number4;
$totalScore = 0; // Initialize total score
?>
<form method="post">
<p>Question 1: What is <?php echo $number1; ?> + <?php echo $number2; ?>?</p>
<input type="text" name="answerIn1" placeholder="Enter your answer" required><br>
<p>Question 2: What is <?php echo $number3; ?> + <?php echo $number4; ?>?</p>
<input type="text" name="answerIn2" placeholder="Enter your answer" required><br>
<div class="centered-container-ForSubmitBtn">
<input type="submit" name="submit_answer" value="Submit" style="border: 1px solid;">
</div>
</form>
<?php
// Check if the form has been submitted
if (isset($_POST['submit_answer'])) {
$userAnswers1 = $_POST['answerIn1']; // Get the user's answers from the submitted form for Q1
$userAnswers2 = $_POST['answerIn2']; // Get the user's answers from the submitted form for Q2
$user_name = $_SESSION['user_name']; // Retrieve the username from the session
if ($userAnswers1 == $answer1) {
$totalScore = $totalScore + 1;
}
if ($userAnswers2 == $answer2) {
$totalScore = $totalScore + 1;
}
global $wpdb; // Access the global $wpdb object
$table_name = $wpdb->prefix . 'My_user_test_info'; // Prefix the table name with the WordPress prefix
// Prepare and execute the SQL query to insert the data into the database
$data = array(
'user_names' => $user_name,
'test_scores' => $totalScore);
$wpdb->insert($table_name, $data);
//Direct the user to a different webpage
wp_redirect(admin_url('/results-page/'));
}
?>
<?php
get_footer();
?>
I don't have any other problems except for the one mentioned above.
答案1
得分: 1
您的数字在用户提交表单时会发生变化。您需要将这些数字存储在会话中,以便与用户输入进行匹配。
最终的代码将如下所示。
<?php
session_start();
get_header();
// 检查表单是否已提交
if (isset($_POST['submit_answer'])) {
$totalScore = 0; // 初始化总分
$userAnswers1 = $_POST['answerIn1']; // 从提交的表单中获取用户对Q1的答案
$userAnswers2 = $_POST['answerIn2']; // 从提交的表单中获取用户对Q2的答案
$user_name = $_SESSION['user_name']; // 从会话中检索用户名
if ($userAnswers1 == $_SESSION['answer1']) {
$totalScore = $totalScore + 1;
}
if ($userAnswers2 == $_SESSION['answer2']) {
$totalScore = $totalScore + 1;
}
global $wpdb; // 访问全局的 $wpdb 对象
$table_name = $wpdb->prefix . 'My_user_test_info'; // 使用WordPress前缀添加表名
// 准备并执行SQL查询以将数据插入数据库
$data = array(
'user_names' => $user_name,
'test_scores' => $totalScore
);
$wpdb->insert($table_name, $data);
// 将用户重定向到不同的网页
wp_redirect(admin_url('/results-page/'));
} else {
// 生成一些随机数字
$number1 = rand(1, 10); // 生成一个介于1和10之间的随机数
$number2 = rand(1, 10); // 生成另一个介于1和10之间的随机数
$number3 = rand(1, 10); // 生成另一个介于1和10之间的随机数
$number4 = rand(1, 10); // 生成另一个介于1和10之间的随机数
// 计算两个问题的答案
$answer1 = $number1 + $number2;
$answer2 = $number3 + $number4;
$_SESSION['answer1'] = $answer1;
$_SESSION['answer2'] = $answer2;
}
?>
<form method="post">
<p>问题1:<?php echo $number1; ?> + <?php echo $number2; ?> 是多少?</p>
<input type="text" name="answerIn1" placeholder="输入您的答案" required><br>
<p>问题2:<?php echo $number3; ?> + <?php echo $number4; ?> 是多少?</p>
<input type="text" name="answerIn2" placeholder="输入您的答案" required><br>
<div class="centered-container-ForSubmitBtn">
<input type="submit" name="submit_answer" value="提交" style="border: 1px solid;">
</div>
</form>
<?php
get_footer();
?>
英文:
Your numbers is getting changed every time user submits the form. You will need to store the numbers in session for matching with the users input.
The final code will look like this.
<?php
session_start();
get_header();
// Check if the form has been submitted
if (isset($_POST['submit_answer'])) {
$totalScore = 0; // Initialize total score
$userAnswers1 = $_POST['answerIn1']; // Get the user's answers from the submitted form for Q1
$userAnswers2 = $_POST['answerIn2']; // Get the user's answers from the submitted form for Q2
$user_name = $_SESSION['user_name']; // Retrieve the username from the session
if ($userAnswers1 == $_SESSION['answer1']) {
$totalScore = $totalScore + 1;
}
if ($userAnswers2 == $_SESSION['answer2']) {
$totalScore = $totalScore + 1;
}
global $wpdb; // Access the global $wpdb object
$table_name = $wpdb->prefix . 'My_user_test_info'; // Prefix the table name with the WordPress prefix
// Prepare and execute the SQL query to insert the data into the database
$data = array(
'user_names' => $user_name,
'test_scores' => $totalScore);
$wpdb->insert($table_name, $data);
//Direct the user to a different webpage
wp_redirect(admin_url('/results-page/'));
}else{
//GENERATING SOME RANDOM NUMBERS
$number1 = rand(1, 10); // Generate a random number between 1 and 10
$number2 = rand(1, 10); // Generate another random number between 1 and 10
$number3 = rand(1, 10); // Generate another random number between 1 and 10
$number4 = rand(1, 10); // Generate another random number between 1 and 10
// Calculate the answers for the two questions
$answer1 = $number1 + $number2;
$answer2 = $number3 + $number4;
$_SESSION['answer1'] = $answer1;
$_SESSION['answer2'] = $answer2;
}
?>
<form method="post">
<p>Question 1: What is <?php echo $number1; ?> + <?php echo $number2; ?>?</p>
<input type="text" name="answerIn1" placeholder="Enter your answer" required><br>
<p>Question 2: What is <?php echo $number3; ?> + <?php echo $number4; ?>?</p>
<input type="text" name="answerIn2" placeholder="Enter your answer" required><br>
<div class="centered-container-ForSubmitBtn">
<input type="submit" name="submit_answer" value="Submit" style="border: 1px solid;">
</div>
</form>
<?php
get_footer();
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论