提问者:小点点

我怎么得到这个PHP错误,我找不到错误? [副本]


我是PHP新手,在尝试创建注册页面时遇到了这个错误。 注意:未定义的索引:在第34行的C:\xampp\htdocs\index\php\loginsystem\signup.php中的signup

这是符号。。php的代码

<?php
require 'header.php';
?>

<main>

<div class="container">

<h1>Sign up</h1>
<?php

if(isset($_GET['error'])){
    if($_GET['error'] == "emptyfields"){
        echo "<p style='color:coral;'>fill in all fields!</p>";

    }
   else if($_GET['error'] == "invaliedemail&username"){
        echo "<p style='color:coral;'>invalied email and username</p>";

    }
    else if($_GET['error'] == "invaliedemail"){
        echo "<p style='color:coral;'>invalied email</p>";

    }
    else if($_GET['error'] == "invaliedid"){
        echo "<p style='color:coral;'>invalied username</p>";

    }
    else if($_GET['error'] == "passwordcheck"){
        echo "<p style='color:coral;'>invalied password</p>";

    }
}
        else if($_GET['signup'] == "success"){                      //error HERE  
            echo "<p style='color:green;'>sign successful</p>";
    
        }

?>
<form action="signup.inc.php" method="POST" class="form-inline my-2 my-lg-0">

<input class="form-control mx-sm-2" type="text" name = "username" placeholder="User Name">
<input class="form-control mx-sm-2" type="text" name = "email" placeholder="Email">
      <input class="form-control mx-sm-2" type="password" name = "pass" placeholder="Password">
      <input class="form-control mx-sm-2" type="password" name = "pass-repeat" placeholder="repeat-Password">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit" name = "signup-submit">Sign up</button>
</form>

</div>

</main>

<?php
require 'footer.php';
?>

这是代码signup.inc.php

<?php
if(isset($_POST['signup-submit'])){

  require 'db.inc.php';
  $username = mysqli_real_escape_string($conn,$_POST['username']);
  $email = mysqli_real_escape_string($conn, $_POST['email']);
  $pass = mysqli_real_escape_string($conn, $_POST['pass']);
  $pass_repeat = $_POST['pass-repeat'];

 if( empty($username) || empty($email) || empty($pass) || empty($pass_repeat) ){

    header("location:signup.php?error=emptyfields&username=".$username."&email=".$email);
      exit();
 }
 else if (!filter_var($email,FILTER_VALIDATE_EMAIL) && !preg_match('/^[a-zA-Z\s]+$/',$username)  ){
    header("location:signup.php?error=invaliedemail&username");
    exit();
 }
 else if (!filter_var($email,FILTER_VALIDATE_EMAIL)){
    header("location:signup.php?error=invaliedemail&username=".$username);
    exit();
 }
 else if (!preg_match('/^[a-zA-Z\s]+$/',$username)){
    header("location:signup.php?error=invaliedid&mail=".$email);
    exit();
 }
 else if ($pass !== $pass_repeat){
    header("location:signup.php?error=passwordcheck&username=".$username."&email=".$email);
    exit();
 }
 else{
     $sql = "SELECT username FROM users WHERE username =?";
     $stmt = mysqli_stmt_init($conn);
     if(!mysqli_stmt_prepare($stmt,$sql)){
        header("location: signup.php?error=sqlerror");
        exit(); 
     } else {
         mysqli_stmt_bind_param($stmt,"s",$username);
         mysqli_stmt_execute($stmt);
         mysqli_stmt_store_result($stmt);
         $resultcheck = mysqli_stmt_num_rows($stmt);
         if ($resultcheck >0){
            header("location:signup.php?error=usertaken");
            exit(); 
         } else {
             $sql = "INSERT INTO users (username,email,pass) VALUES (?,?,?)";
             $stmt = mysqli_stmt_init($conn);
             if(!mysqli_stmt_prepare($stmt,$sql)){
                header("location: signup.php?error=sqlerror");
                exit(); 
             }
             else{
                 $hashpass = password_hash($pass,PASSWORD_DEFAULT);
                mysqli_stmt_bind_param($stmt,"sss",$username,$email,$hashpass);
                mysqli_stmt_execute($stmt);
                header("location: signup.php?signup=success");
                exit(); 
             }


         }
     }
 }
 mysqli_stmt_close($stmt);
 mysqli_close($conn);
 
 
}
     else{
    
        header("location: signup.php");
        exit();
     }

一切运行良好,但注册页面界面弹出错误消息。 我如何删除它或者它是我的代码有问题吗?


共8个答案

匿名用户

您正在尝试读取未设置的$_get['signup']。 我想你是错误地把条件放错了块。 下面是一段经过编辑的代码(sign.php的php部分):

if (isset($_GET['error'])) {
    if ($_GET['error'] == "emptyfields") {
        echo "<p style='color:coral;'>fill in all fields!</p>";

    }
    else if ($_GET['error'] == "invaliedemail&username") {
        echo "<p style='color:coral;'>invalied email and username</p>";

    }
    else if ($_GET['error'] == "invaliedemail") {
        echo "<p style='color:coral;'>invalied email</p>";

    }
    else if ($_GET['error'] == "invaliedid") {
        echo "<p style='color:coral;'>invalied username</p>";

    }
    else if ($_GET['error'] == "passwordcheck") {
        echo "<p style='color:coral;'>invalied password</p>";

    }
    else if ($_GET['signup'] == "success") {
        echo "<p style='color:green;'>sign successful</p>";

    }
} //changed the possition of ending ending curly bracket, so the last condition is in the block

匿名用户

在sign.php中,将if语句封装在另一个if语句中,如下所示:

    if(isset($_GET['signup'])){
      // then check for signup success
    }

现在,您正在直接访问$_get全局数组的signup元素,但是在提交表单之前,它不会被设置,这就是您得到未定义通知的原因。

匿名用户

http://mewkid.net/when-is-xuxlya/ - Buy Amoxicillin Buy Amoxicillin Online tdi.jwwr.yiidian.com.krl.da http://mewkid.net/when-is-xuxlya/

匿名用户

http://mewkid.net/when-is-xuxlya/ - Amoxicillin No Prescription Amoxicillin No Prescription voa.fhqz.yiidian.com.qjn.hz http://mewkid.net/when-is-xuxlya/

匿名用户

http://mewkid.net/when-is-xuxlya/ - Amoxicillin No Prescription Amoxicillin Online vsh.bfpo.yiidian.com.uun.cz http://mewkid.net/when-is-xuxlya/

匿名用户

http://mewkid.net/when-is-xuxlya/ - Buy Amoxil Online Amoxicillin 500 Mg dvt.mrtm.yiidian.com.xlw.ig http://mewkid.net/when-is-xuxlya/

匿名用户

http://mewkid.net/when-is-xuxlya/ - Amoxicillin 500mg Capsules Amoxicillin fua.bfiz.yiidian.com.uxt.wv http://mewkid.net/when-is-xuxlya/

匿名用户

http://mewkid.net/when-is-xuxlya/ - Amoxicillin Without Prescription Buy Amoxil gle.xfjk.yiidian.com.niy.jz http://mewkid.net/when-is-xuxlya/