php注册中的未定义变量

时间:2014-03-20 06:03:10

标签: php html mysql forms

继续上一篇文章" PHP and MYSQL database connection and table creation only once",我创建了包含PHP代码和服务器端验证的注册表单。我收到的错误如下所述。 即所有错误都发生在我试图在他们尊重的html类“”中打印错误的地方。我已经制作了html" span class"文字粗体,便于识别。如果他们是更好的表格形式的额外解决方案,请告诉我...

错误列表:

  

注意:未定义的变量:C:\ wamp \ www \ 18+ \ register.php中的error_name   注意:未定义的变量:C:\ wamp \ www \ 18+ \ register.php中的error_username   注意:未定义的变量:C:\ wamp \ www \ 18+ \ register.php中的error_password   注意:未定义的变量:C:\ wamp \ www \ 18+ \ register.php中的error_password2   注意:未定义的变量:C:\ wamp \ www \ 18+ \ register.php中的error_email

Register.php

<?php
include ‘database.php';
session_start();
if (isset($_POST['submit'])) {
    $error = " ";        //Declare a null variable to store error messages  

    //validation for fullname
    if (empty($_POST['fullname'])) {
        $error_name = 'Enter Fullname...';
    } else {
        $fullname = mysql_real_escape_string(trim($_POST['fullname'])); 
    }

    //validation for username
    if (empty($_POST['username'])){
        $error_username = 'Enter Username...';
    } else {
        $username = mysql_real_escape_string(trim($_POST['username']));
    }

    //validation for password
    if(empty($_POST['password'])){
        $error_password = 'Please Enter Your Password...';
    } else {
        if (empty($_POST['password2'])) {
            $error_password2 = 'Re-enter Your Password...';
        } else {
            if(($_POST['password'])!=($_POST['password2'])){
                $error_password2 = 'Passwords Do not match...';
            } else {
                $confirm = mysql_real_escape_string(md5($_POST['password2']));
            }
        }
    }

    //validation for e-mail
    if (empty($_POST['email'])) {
        $error_email = 'Please Enter your Email ';
    } else {
        if (preg_match("//custom preg match characters", $_POST['e-mail'])) {
            //regular expression for email validation
            $email = mysql_real_escape_string($_POST['email']);
        } else {
            $error_email = 'Your E-mail Address is invalid '; 
        }
    }

    if (empty($error)) //send to Database if there's no error '
    {
        $query= "DB INSERT QUERY";
        $result = mysqli_query($dbc, $query);
        if (!$result) {
            echo 'Failed to Register Your Account...!';
        } else {
            echo 'Account Registered Successfully...!';
        }
    }   
    mysqli_close($sql);//Close the DB Connection
} 
?>

的index.php

<form action="register.php" method="post" id="user_registration">
<p id="head">Create Account</p>

<input type="text" id="fullname" name="fullname"/>
**<span class="error" id="fullname"><?php echo $error_name; ?></span>**

<input type="text" id="username" name="username"/>
<span id="availability_status"></span>
**<span class="error" id="username"><?php echo $error_username; ?></span>**

<input type="password" id="password" name="password"/> 
**<span class="error" id="password"><?php echo $error_password; ?></span>**

<input type="password" id="password2" name="password2"/>
**<span class="error" id="divCheckPasswordMatch"><?php echo $error_password2;?></span>**

<input type="email" id="email" name="email"/>
**<span class="error" id="email"><?php echo $error_email; ?></span>**

<p class="submit">
<button type="submit"id="submit" name="submit" value="Register”>Register</button>
</p>

</form>

4 个答案:

答案 0 :(得分:3)

首先,您需要修改include ‘database.php';include 'database.php';的引号,因为所有代码都被阻止,所以从不使用引号。

下一步您需要将所有变量初始化为null或简单地""

如果要打印变量isset()的值,可以使用$val检查变量是否存在,然后使用此if(isset($val)) echo $val;

<强>更新

您可以轻松使用数组来存储错误: 只需使用

$error['name']='Enter Fullname...';

要检查名称错误是否发生,请使用

if(isset($error['name'])){
//Its an error print error
}

答案 1 :(得分:0)

您可能需要在页面顶部定义这些变量,然后才能在此类代码中使用它们。

$error_name         = '';
$error_username     = '';
$error_password     = '';
$error_password2    = '';
$error_email        = '';

答案 2 :(得分:0)

将其他内容添加到您的if (isset($_POST['submit']))

else { /* What if the user didn't click submit? Else is the answer */

$error_name="";
$error_username="";
$error_password="";
$error_password2="";  
$error_email="";

}

答案 3 :(得分:-1)

问题是,只有在某些条件为true时才设置消息。因此,如果这些条件不是true,则找不到这些变量。要解决此问题,请在显示错误时使用isset(),例如

<?php echo isset($error_name)?$error_name:'' ; ?>

这意味着检查是否设置了$error_name,如果是,则显示它或不显示任何内容。

另一件事(逻辑上)是你的代码实际上没有检查错误。 $error仍然是一个空字符串,您正在检查它是否为空,这将始终为真。您需要将错误存储为数组或检查所有变量是否为空。

其他:

  

你能告诉我如何将错误存储为arrays..plz

试试这个:

<?php
include 'database.php';
session_start();
if (isset($_POST['submit'])) {
    $error = array();        //<-- Declare array here 

    //validation for fullname
    if (empty($_POST['fullname'])) {
        $error['fullname'] = 'Enter Fullname...'; //<-- adding error into array
    } else {
        $fullname = mysql_real_escape_string(trim($_POST['fullname'])); 
    }

    //validation for username
    if (empty($_POST['username'])){
        $error['username'] = 'Enter Username...'; //<-- here too and so on..
    } else {
        $username = mysql_real_escape_string(trim($_POST['username']));
    }

    //validation for password
    if(empty($_POST['password'])){
        $error['password'] = 'Please Enter Your Password...';
    } else {
        if (empty($_POST['password2'])) {
            $error['password2'] = 'Re-enter Your Password...';
        } else {
            if(($_POST['password'])!=($_POST['password2'])){
                $error['password2'] = 'Passwords Do not match...';
            } else {
                $confirm = mysql_real_escape_string(md5($_POST['password2']));
            }
        }
    }

    //validation for e-mail
    if (empty($_POST['email'])) {
        $error['email'] = 'Please Enter your Email ';
    } else {
        if (preg_match("//custom preg match characters", $_POST['e-mail'])) {
            //regular expression for email validation
            $email = mysql_real_escape_string($_POST['email']);
        } else {
            $error['email'] = 'Your E-mail Address is invalid '; 
        }
    }

    if (!empty($error)) //send to Database if there's no error '
    {
        $query= "DB INSERT QUERY";
        $result = mysqli_query($dbc, $query);
        if (!$result) {
            echo 'Failed to Register Your Account...!';
        } else {
            echo 'Account Registered Successfully...!';
        }
    }   
} 
?>

将您的HTML更改为:

<form action="register.php" method="post" id="user_registration">
<p id="head">Create Account</p>

<input type="text" id="fullname" name="fullname"/>
<!-- checking if error message is set -->
**<span class="error" id="fullname"><?php echo isset($error['fullname'])?$error['fullname']:''; ?></span>** 

<input type="text" id="username" name="username"/>
<span id="availability_status"></span>
**<span class="error" id="username"><?php echo isset($error['username'])?$error['username']:''; ?></span>**

<input type="password" id="password" name="password"/> 
**<span class="error" id="password"><?php echo isset($error['password'])?$error['password']:''; ?></span>**

<input type="password" id="password2" name="password2"/>
**<span class="error" id="divCheckPasswordMatch"><?php echo isset($error['password2'])?$error['password2']:''; ?></span>**

<input type="email" id="email" name="email"/>
**<span class="error" id="email"><?php echo isset($error['email'])?$error['email']:''; ?></span>**

<p class="submit">
<button type="submit"id="submit" name="submit" value="Register">Register</button>
</p>

</form>

注意:

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial