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 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)