嵌套 if 语句混淆 - 'Forgot my password'

Nested if statements confusion - 'Forgot my password'

有没有办法快速识别嵌套 if 语句的问题,不断出现错误说一个太多 } 然后在删除某些括号时得到 'This count is unaware if curly braces are inside of a string':

想知道是否有人可以指导我如何正确调试它

我的代码是重置用户密码

这是我的代码:

<?php 
if (!$username && !$id){
    if($_POST['resetbtn']){

        // get the form data
        $username = $_POST['username'];
        $email  = $_POST['email'];

        // make sure info provided
        if($username){
            if($email){
                if((strlen($email) > 7) && (strstr($email, "@")) && (strstr($email, ".")) ){
                    require("connect.php");
                    $find_email = $mysqli->query("SELECT * FROM `users` WHERE `username`='$username'");
                    if($find_email->num_rows){
                        while($row = $find_email->fetch_array(MYSQLI_ASSOC)){
                        $email = $row['email'];

                        if($dbemail == $email){
                            $options = [
                                'cost' => 10,
                                'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
                            ];
                            $pass = rand();
                            $pass= password_hash($pass, PASSWORD_BCRYPT, $options);

                            $query = ("UPDATE `users` SET `password` =? WHERE `username` =?");
                            $stmt = $mysqli->prepare($query);
                            $stmt->bind_param('ss',$pass,$username);
                            $stmt->execute();
                            if ($stmt->errno) {
                              echo "There was an error updating" . $stmt->error;
                            }
                            else echo "Updated {$stmt->affected_rows} rows";
                            $stmt->close();

                            // make sure the password was changed

                            $check_update = $mysqli->query("SELECT * FROM `users` WHERE `username`='$username' AND `password` ='$pass'");
                            if($check_update->num_rows){

                                $webmaster = "fortunefillyvip@homtail.com";
                                $headers = "From: FortuneFilly<$webmaster>";
                                $subject = "Your New Password";
                                $message = "Hello. Your password has been reset. Password: $pass";


                                if(mail($email, $subject, $message, $headers)){
                                    echo"Your password has been reset. An email has been sent with new password.";
                                }
                                else
                                    echo"An error has occured and your email was not sent containing your new password";
                            }
                            else
                                echo"An error has occurred and the password was not reset";
                        }
                        else
                            echo"You entered the wrong email address";

                    }
                    }
                    $result->free();
                    }
                    else
                        echo"The username was not found";

                    mysqli_close();
                }
                else
                    echo "Please enter a valid email";
            }
            else
                echo"Please enter your email";
        }
        else
            echo"Please enter your username";

    }


    echo "<form action='fogotPass.php' method='POST'>
    <table>
    <tr>
        <td>Username:</td>
        <td><input type='text' name='user'</td>
    </tr>
    <tr>
        <td>Email:</td>
        <td><input type='text' name='email'</td>
    </tr>
    <tr>
        <td></td>
        <td><input type='submit' name='resetbtn' value='Reset password'</td>
    </tr>
    </table>
    </form>";
}
else
    echo "Please log out to view this page."
?>

用的好IDE,只有记事本才会犯这样的错误

另外 if (!$username && !$id){ 变量未定义。

你的脚本做的太多了,查询数据库,验证并打印 HTML 表单。

你必须把事情分开一点。

看看:

  • 您可以看到格式错误的标签
  • 你 post 到同一个脚本但你有 action='forgotPass.php'

然后 PHP 更多问题:

  • 同样,使用 IDE 你可以看到其他警告
  • 正如我所见,你有一个额外的“}”,它是最后一个,在其他之上 声明 .
  • 您的第一个 if 语句 ( if (!$username && !$id){ ) 刚刚关闭 之前 ( echo "form ).
  • 而且你没有在最后一条语句的末尾加上分号(;)。

第 1 行多了一个 } 括号。 97

只需删除“$result->free();”之前的右花括号之一即可。