如果在未使用$ _GET ['']表单时如何处理“未定义索引”错误?

时间:2017-05-20 06:55:07

标签: php html css mysqli error-handling

我正在为Login Password Recovery编写代码,因此,我需要一个名为'recoverykey'的$ _GET值。每当我在没有定义 recoverykey 的情况下访问WebPage时,它会告诉我未定义的索引错误。当然,会出现错误,但我不需要表达 recoverykey 的值,那么如何删除该错误呢?

当我在网址上时,错误看起来像这样: localhost / websites / index.php

Error looks like this

当我在网址上时,错误消失了: localhost / websites / index.php?recoverykey = 39238579857

代码如下:

<?php

        $change_code = $_GET['recoverykey'];

        $sqlQuery = "SELECT * FROM RegisteredMembers WHERE RecoveryCode='$change_code'";
        $result = mysqli_query($conn,$sqlQuery);
        $fetch_result = mysqli_fetch_array($result);
        $row_count = mysqli_num_rows($result);
        if($row_count==1 && !$change_code==0){

                $error = false;

                $passError = null;
                $output = null;
                $errorMsg = null;

                if(isset($_POST['btn-changepass-submit'])){

                    $password = $_POST['change-pass'];

                    // Password Validation
                    if(empty($password)){
                        $error = true;
                        $passError = "Please write your new password.";
                    } else if(strlen($password)<8){
                        $error = true;
                        $passError = "Passwords should atleast be 8 chars long.";
                    }

                    // If no error, continue
                    if(!$error){
                        /*$fetch_result['userEmail'] = $email;*/
                        $dtbQuery = "UPDATE RegisteredMembers SET password='$password' WHERE RecoveryCode='$change_code'";
                        $new_result = mysqli_query($conn,$dtbQuery);
                        if($new_result){
                            $errorType = "success";
                            $errorMsg = "Successfully changed password. <a href=#login-login>Login here</a>.";

                            $dbQuery = "UPDATE RegisteredMembers SET RecoveryCode='0' WHERE RecoveryCode='$change_code'";
                            $new_output = mysqli_query($conn,$dbQuery);

                        } else{
                            $errorType = "danger";
                            $errorMsg = "There seems to be an error. Please try again!";
                        }
                    }

                }

                ?>
                    <div id="login-passchange">
                        <h2>Change your Password</h2>
                        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?recoverykey=<?php echo $change_code ?>#login-passchange">
                            <span class="<?php echo $errorType; ?>-msg">
                                <?php
                                    if($output){
                                        echo $errorMsg;
                                    } else{
                                        echo $errorMsg;
                                    }
                                ?>
                            </span>
                            <label><span class="danger-msg"><?php echo $passError; ?></span><input type="password" placeholder="Write your New Password" maxlength="255" name="change-pass" required /></label>
                            <input type="submit" value="Set new Password" name="btn-changepass-submit" />
                        </form>
                        <div class="lower-part">
                            <a class="login-link" href="#login-login">Password Found? Login</a>
                            <br />
                            <a class="home-link" href="#home">Go back</a>
                        </div>
                    </div>
                <?php
        } else{
            ?>
                <div id="login-passchange">
                        <h2>Error 404!</h2>
                </div>
            <?php
        }

    ?>

代码在密码更改方面成功,但我不希望出现错误消息。更重要的是,我尝试在StackOverflow上找到这个,但这个问题没有具体的答案。所以,它永远不会重复。

提前致谢,求助!

1 个答案:

答案 0 :(得分:0)

使用此

$change_code = isset($_GET['recoverykey']) ? $_GET['recoverykey'] : '';