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

Why is my PHP conditional statement not working properly?

提问人:Saam4 提问时间:6/22/2023 最后编辑:DarkBeeSaam4 更新时间:6/22/2023 访问量:70

问:

下面 PHP 代码的目的是创建一个由两个问题组成的数学测验。用户将回答这两个问题,然后单击提交按钮。

点击提交按钮后,用户的答案将与正确答案进行比较(每个问题将单独比较),然后计算总分。$totalScore

最后,用户的数据将入到数据库中,然后用户将被定向到不同的网页。

我唯一的问题是 的值,这个值要么是 1 要么是 0,无论用户回答所有问题都是对的还是错的。这是随机的。我不知道为什么。$totalscore

  • 如果用户正确回答了两个问题,则 的值应为 = 2$totalscore
  • 如果用户正确回答了其中一个问题,则 E 的值应为 = 1$totalscor
  • 如果用户错误地回答了两个问题,则 的值应为 = 0$totalscore

但事实并非如此。请帮忙

<?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();
?>

除了上面提到的问题,我没有任何其他问题。

php if-语句

评论

5赞 DarkBee 6/22/2023
您没有跟踪/存储原始号码。这些将随着每个请求而变化,因此无法跟踪/验证是否有任何用户给出了正确的答案$number1 = rand(1, 10);
1赞 Barmar 6/22/2023
在责备条件语句之前,您应该回显您正在测试的变量。
0赞 Zaid Bin Khalid 6/22/2023
提交时,应将旧的兰特数字变量与表单一起发送。从技术上讲,当您加载页面时,n1 和 n2 与您在提交表单后比较的数字不同。在您的帖子请求中发送旧号码,然后进行比较。

答:

1赞 Ashis Kumar 6/22/2023 #1

每次用户提交表单时,您的号码都会更改。您需要将数字存储在会话中,以便与用户输入匹配。

最终代码将如下所示。

<?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();
?>

评论

0赞 Saam4 6/23/2023
谢谢Ashis Kumar和其他所有人。这解决了我的问题。你们比 ChatGPT 好 10 倍