变量未定义

时间:2015-11-16 19:58:03

标签: php variables

我设置错误变量以包含所有错误,如果用户离开任何输入并且他们试图在div中回显它但是我收到此消息:

"注意:未定义的变量:第202行和第34行的C:\ xampp \ htdocs \ signup.php中的错误;

虽然我先在第一个php标签中定义了这个变量,但我仍然得到错误..

<?php 

session_start();

if(isset($_POST['signup'])){

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $pass = $_POST['pass'];
    $cpass = $_POST['cpass'];

    $error="";

        if(!$fname){

        $error.="<br />Please enter your First Name";

        }

        if(!$lname){

           $error.="<br />Please enter your Last Name";

        }

        if(!$email){

            $error.="<br />Please enter your Email Adress";

        }

        if(!$pass && strlen($_POST['pass'])<8){

            $error.="<br />Please enter your Password with atleast 8 Characters.";

        }

        if(!$cpass){

            $error.="<br />Please enter your Confirmed Password";

        }

        if($pass != $cpass){

            $error.="<br />Your Passwords don't match";

        }

        if(!$error){

            $link = mysqli_connect("localhost", "root", "", "shop");

            $query = "SELECT * FROM users WHERE email='".mysqli_real_escape_string($link, $email)."'";

            $result = mysqli_query($link, $query);

            $results = mysqli_num_rows($result);

            if($results == 1){

                $error = "This Email is already Registered. Do you want to <a href='login.php'>Login </a>?";

            }else{

                $query = "INSERT INTO users (fname, lname, email, password) VALUES ('".$fname."', '".$lname."', '".mysqli_real_escape_string($link, $email)."', '".md5(md5($email).$pass)."')";

                $result = mysqli_query($link, $query);

            if($result == FALSE){

                $error = "Couldn't Connect";

            }else{

                $_SESSION['id']=mysqli_insert_id($link);

                print_r($_SESSION);

                /* Redirect to Logged In Page */

            }
        }

        }else{

            $error = "There were following error(s) in your Sign Up Details.".$error;

            echo '<div class="alert alert-danger">'.$error.'</div>';

        }

}

&GT;

&#39; $错误&#39;&#39 ;;                 }            ?&GT;
     <form method="post" action="">

       <div class="form-group inputs">

          <span>First Name: </span><input type="text" name="fname" class="form-control" style="margin-right:20px" />

          <span>Last Name: </span><input type="text" name="lname" class="form-control" />

       </div> <!-- end form-group Div -->

       <div class="form-group inputs">

          <span>Email: </span><input type="text" style="width:350px" name="email" class="form-control" />

       </div> <!-- end form-group Div -->

       <div class="form-group inputs">

          <span style="margin-left:-33px;">Password: </span><input type="password" style="width:350px;" name="pass" class="form-control" />

       </div> <!-- end form-group Div -->

       <div class="form-group inputs">

          <span style="margin-left:-102px;">Confirm Password: </span><input type="password" style="width:350px" name="cpass" class="form-control" />

       </div> <!-- end form-group Div -->

       <div style="margin:10px auto 10px auto; width:70%; text-align:center">

          <span>By registering your Account in our Online Store, You agree to our <a href="#">Terms And Conditions</a> and Confirm that you have read our <a href="#">User Guide</a>.</span>

       </div>

       <div class="form-group center">

          <input type="submit" value="Sign Up" name="signup" class="btn btn-success btn-lg">

     </form>

1 个答案:

答案 0 :(得分:2)

问题是$ error只在if (isset($_POST['signup'])) {条件下定义。

您需要在外面定义它,或者只是在您尝试使用它时检查它是否已设置,例如

if (isset($error)) {

如果你想检查它也不是空白,你可以

if (isset($error) && $error !== '') {

虽然

if (isset($error) && $error) {

也可以,因为非空白,非零值将等于true。但我认为第一种选择更具可读性。