为什么我的PHP条件语句不能正常工作?

huangapple go评论66阅读模式
英文:

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 $totalscore 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

&lt;?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
?&gt;
&lt;form method=&quot;post&quot;&gt;
&lt;p&gt;Question 1: What is &lt;?php echo $number1; ?&gt; + &lt;?php echo $number2; ?&gt;?&lt;/p&gt;
&lt;input type=&quot;text&quot; name=&quot;answerIn1&quot; placeholder=&quot;Enter your answer&quot; required&gt;&lt;br&gt;
&lt;p&gt;Question 2: What is &lt;?php echo $number3; ?&gt; + &lt;?php echo $number4; ?&gt;?&lt;/p&gt;
&lt;input type=&quot;text&quot; name=&quot;answerIn2&quot; placeholder=&quot;Enter your answer&quot; required&gt;&lt;br&gt;
&lt;div class=&quot;centered-container-ForSubmitBtn&quot;&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit_answer&quot; value=&quot;Submit&quot; style=&quot;border: 1px solid;&quot;&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;?php
// Check if the form has been submitted
if (isset($_POST[&#39;submit_answer&#39;])) {
$userAnswers1 = $_POST[&#39;answerIn1&#39;]; // Get the user&#39;s answers from the submitted form for Q1
$userAnswers2 = $_POST[&#39;answerIn2&#39;]; // Get the user&#39;s answers from the submitted form for Q2
$user_name = $_SESSION[&#39;user_name&#39;]; // 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-&gt;prefix . &#39;My_user_test_info&#39;; // Prefix the table name with the WordPress prefix
// Prepare and execute the SQL query to insert the data into the database
$data = array(
&#39;user_names&#39; =&gt; $user_name,
&#39;test_scores&#39; =&gt; $totalScore);
$wpdb-&gt;insert($table_name, $data);
//Direct the user to a different webpage
wp_redirect(admin_url(&#39;/results-page/&#39;));
}
?&gt;
&lt;?php
get_footer();
?&gt;

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.

&lt;?php
session_start();
get_header();
// Check if the form has been submitted
if (isset($_POST[&#39;submit_answer&#39;])) {
$totalScore = 0; // Initialize total score
$userAnswers1 = $_POST[&#39;answerIn1&#39;]; // Get the user&#39;s answers from the submitted form for Q1
$userAnswers2 = $_POST[&#39;answerIn2&#39;]; // Get the user&#39;s answers from the submitted form for Q2
$user_name = $_SESSION[&#39;user_name&#39;]; // Retrieve the username from the session
if ($userAnswers1 == $_SESSION[&#39;answer1&#39;]) {
$totalScore = $totalScore + 1;
}
if ($userAnswers2 == $_SESSION[&#39;answer2&#39;]) {
$totalScore = $totalScore + 1;
}
global $wpdb; // Access the global $wpdb object
$table_name = $wpdb-&gt;prefix . &#39;My_user_test_info&#39;; // Prefix the table name with the WordPress prefix
// Prepare and execute the SQL query to insert the data into the database
$data = array(
&#39;user_names&#39; =&gt; $user_name,
&#39;test_scores&#39; =&gt; $totalScore);
$wpdb-&gt;insert($table_name, $data);
//Direct the user to a different webpage
wp_redirect(admin_url(&#39;/results-page/&#39;));
}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[&#39;answer1&#39;] = $answer1;
$_SESSION[&#39;answer2&#39;] = $answer2;
}
?&gt;
&lt;form method=&quot;post&quot;&gt;
&lt;p&gt;Question 1: What is &lt;?php echo $number1; ?&gt; + &lt;?php echo $number2; ?&gt;?&lt;/p&gt;
&lt;input type=&quot;text&quot; name=&quot;answerIn1&quot; placeholder=&quot;Enter your answer&quot; required&gt;&lt;br&gt;
&lt;p&gt;Question 2: What is &lt;?php echo $number3; ?&gt; + &lt;?php echo $number4; ?&gt;?&lt;/p&gt;
&lt;input type=&quot;text&quot; name=&quot;answerIn2&quot; placeholder=&quot;Enter your answer&quot; required&gt;&lt;br&gt;
&lt;div class=&quot;centered-container-ForSubmitBtn&quot;&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit_answer&quot; value=&quot;Submit&quot; style=&quot;border: 1px solid;&quot;&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;?php
get_footer();
?&gt;

huangapple
  • 本文由 发表于 2023年6月22日 14:40:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定