duanjing3656 2017-05-20 06:55
浏览 74
已采纳

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

I'm writing a code for Login Password Recovery and therefore, I needed a $_GET value called 'recoverykey'. Whenever I visit the WebPage without defining recoverykey, it tells me the Undefined Index Error. Of course, the error will occur, but I don't need to express recoverykey's value, so how can I remove that error?

The error looked like this when I'm on the url: localhost/websites/index.php

Error looks like this

The error vanishes when I'm on the url: localhost/websites/index.php?recoverykey=39238579857

The code goes below:

<?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
        }

    ?>

The code goes successful in terms of Password changing but I don't want that Error message to appear. Morever, I tried finding this on StackOverflow where there was no specific answer to this question. So, its never a duplicate.

Thanks in advance, for the help!

</div>
  • 写回答

1条回答 默认 最新

  • doujun5009 2017-05-20 07:01
    关注

    Use this

    $change_code = isset($_GET['recoverykey']) ? $_GET['recoverykey'] : '';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥33 集成BiLSTM模型和GCN模型时,前模型的输出不匹配后模型的输入
  • ¥15 关于液体厚度提取,和铺展轨迹数据化处理
  • ¥20 对方微信是wxid如何添加成好友?或者转换成微信号?
  • ¥15 yolov5实验数据好,但是自己拍摄的照片检测结果差
  • ¥15 做过dali2.0认证的技术,
  • ¥15 一道python难题
  • ¥15 matlab 免疫算法
  • ¥50 可视化界面构建,实现信息模块功能
  • ¥15 基于bert-base-chinese的中文文本处理
  • ¥15 高价求抖音抓包技术,抖音抓包无网络问题