dthhlf1777 2015-11-16 19:58
浏览 16
已采纳

变量undefined [重复]

I'm setting Error Variable to contain all the errors if user left any of the input and them I'm trying to echo it out in a div but I get this message:

"Notice: Undefined variable: error in C:\xampp\htdocs\signup.php on line 202"

Although I've defined this Variable earlier in first php tags but I still get Error..

<?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>';

        }

}

?>

'.$error.'';

            }

       ?&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>
</div>
  • 写回答

1条回答 默认 最新

  • douqulv6059 2015-11-16 20:01
    关注

    The problem is that $error is only defined within the if (isset($_POST['signup'])) { condition.

    You'll need to define it outside, or just check if it's set when you try to use it, e.g.

    if (isset($error)) {

    If you want to check that it's also not blank, you can do

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

    although

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

    will also work, as a non-blank, non-zero value will equate to true. But I think the first option is more readable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c