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'] : '';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵