尽管满足条件,if 语句仍未正确执行

if statement not executing correctly despite conditions being met

问题说明了一切。即使满足条件,代码仍然不是运行。

我使用 echo 测试了正在比较的变量,即使它们相同(值为 1),对于某些 运行,代码仍然不是 运行。

我还检查了我的脚本是否有任何语法错误,但没有发现任何错误。

通过 Whosebug 阅读并没有帮助,而且大部分都与语法错误有关。

无论我做什么,输出仍然是 "condition not met",我可以知道哪里出了问题吗?

下面是代码:

<?php 

include 'database.php';

if(isset($_POST['submit'])){
    $number=$_POST['number'];
    $selected_choice = $_POST['choice'];
    $next=$number+1;

    $query="SELECT* FROM questions";
    $results= mysqli_query($con,$query);
    $total=$results->num_rows;

    $query = "SELECT* FROM `choices` WHERE question_number = $number AND is_correct=1";
    $results= mysqli_query($con,$query);
    $row=$results->fetch_assoc();
    $correct_choice=$row['id'];

    if($correct_choice == $selected_choice){
        echo "conditions same";
    } else{
        echo "conditions not same";
    }

    echo $correct_choice;
    echo $selected_choice;
    echo $score;

}

如果 if 语句中没有打印出任何内容,那么问题肯定出在您的表单提交按钮名称中。我相信,提交按钮的代码应该像下面这样来解决你的问题-

<input type="submit" name="submit" value="Post your data" class="" />

当然,表单方法应该是POST。 检查一下,希望对您来说工作顺利...

已解决,index.php 页中的语法错误。

当我使用 var_dump($correct_choice) 和 var_dump($selected_choice) 时,我得到 string(1) "1" string(2) " 1;".

问题在于多余的;