未定义的变量在PHP注册从 [英] Undefined variable in php registration from

查看:96
本文介绍了未定义的变量在PHP注册从的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续从我以前的帖子 PHP和MYSQL数据库连接和表创建只有一次,我创建了注册表单与PHP代码和服务器端验证。我收到如下所述的一些错误。
,即所有错误都发生在我尝试在他们尊敬的html类中打印错误的地方。为了便于识别,我制作了htmlspan class文本粗体。如果他们是任何额外的解决方案,以更好地表现形式,请让我知道......



错误列表:


注意:未定义变量:error_name在C:\wamp\www\18+\register.php

注意: Undefined variable:error_username in C:\wamp\www\18+\register.php

注意:未定义变量:error_password in C:\wamp\www\18 + \ register.php

注意:未定义的变量:error_password2在C:\ wamp\www\18+\register.php

注意:未定义变量:error_email in C: \wamp\www\18+\\register.php


Register.php

 <?php 
include'database.php';
session_start();
if(isset($ _ POST ['submit'])){
$ error =; //声明一个空变量来存储错误消息

//验证全名
if(empty($ _ POST ['fullname'])){
$ error_name ='Enter全名...';
} else {
$ fullname = mysql_real_escape_string(trim($ _ POST ['fullname']));

$ b $ //验证用户名
if(空($ _ POST ['username'])){
$ error_username ='输入用户名...' ;
} else {
$ username = mysql_real_escape_string(trim($ _ POST ['username']));

$ b $ //验证密码
if(empty($ _ POST ['password'])){
$ error_password ='请输入您的密码.. ';
} else {
if(empty($ _ POST ['password2'])){
$ error_password2 ='重新输入您的密码...'; ($ _ $ POST ['password'])!=($ _ POST ['password2'])){
$ error_password2 ='密码不匹配...
} else {
if ;
} else {
$ confirm = mysql_real_escape_string(md5($ _ POST ['password2']));



$ b //验证电子邮件
if(empty($ _ POST ['email'])){
$ error_email ='请输入您的电子邮件地址';
} else {
if(preg_match(// custom preg match characters,$ _POST ['e-mail'])){
//用于电子邮件验证的正则表达式
$ email = mysql_real_escape_string($ _ POST ['email']);
} else {
$ error_email ='您的电子邮件地址无效';



if(empty($ error))//如果没有错误,发送到数据库'
{
$ query =DB INSERT QUERY;
$ result = mysqli_query($ dbc,$ query);
if(!$ result){
echo'无法注册您的账户...!';
} else {
echo'Account Registered Successfully ...!';
}
}
mysqli_close($ sql); //关闭数据库连接
}
?>

Index.php

 < form action =register.phpmethod =postid =user_registration> 
< p id =head>创建帐户< / p>

< input type =textid =fullnamename =fullname/>
**< span class =errorid =fullname><?php echo $ error_name; ?>< / span> **

< input type =textid =usernamename =username/>
< span id =availability_status>< / span>
**< span class =errorid =username><?php echo $ error_username; ?>< / span> **

< input type =passwordid =passwordname =password/>
**< span class =errorid =password><?php echo $ error_password; ?>< / span> **

< input type =passwordid =password2name =password2/>
**< span class =errorid =divCheckPasswordMatch><?php echo $ error_password2;?>< / span> **

<输入type =emailid =emailname =email/>
**< span class =errorid =email><?php echo $ error_email; ?>< / span> **

< p class =submit>
< button type =submitid =submitname =submitvalue =Register>注册< / button>
< / p>

< / form>


解决方案

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

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

这意味着检查 $ error_name 是如果是的话显示它或不显示任何东西。

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



其他:
$ b


你能告诉我如何将错误作为数组存储?plz

试试这个:

 <?php 
include'database.php';
session_start();
if(isset($ _ POST ['submit'])){
$ error = array(); //< - 在这里声明数组

//验证全名
if(empty($ _ POST ['fullname'])){
$ error ['fullname' ] ='输入全名...'; //< - 在数组
中添加错误} else {
$ fullname = mysql_real_escape_string(trim($ _ POST ['fullname']));

$ b $ //验证用户名
if(empty($ _ POST ['username'])){
$ error ['username'] ='Enter用户名...'; //< - 在这里等等..
} else {
$ username = mysql_real_escape_string(trim($ _ POST ['username']));

$ b $ //验证密码
if(empty($ _ POST ['password'])){
$ error ['password'] ='Please输入你的密码...';
} else {
if(empty($ _ POST ['password2'])){
$ error ['password2'] ='重新输入您的密码...'; ($ _ $ POST ['password'])!=($ _ POST ['password2'])){
$ else ['password2'] ='密码不匹配...';
} else {
$ confirm = mysql_real_escape_string(md5($ _ POST ['password2']));



$ b //验证电子邮件
if(empty($ _ POST ['email'])){
$ error ['email'] ='请输入您的电子邮件地址';
} else {
if(preg_match(// custom preg match characters,$ _POST ['e-mail'])){
//用于电子邮件验证的正则表达式
$ email = mysql_real_escape_string($ _ POST ['email']);
} else {
$ error ['email'] ='您的电子邮件地址无效';



if(!empty($ error))//如果没有错误,发送到数据库'
{
$ query = DB INSERT QUERY;
$ result = mysqli_query($ dbc,$ query);
if(!$ result){
echo'无法注册您的账户...!';
} else {
echo'Account Registered Successfully ...!';
}
}
}
?>

将您的HTML更改为:

 < form action =register.phpmethod =postid =user_registration> 
< p id =head>创建帐户< / p>

< input type =textid =fullnamename =fullname/>
<! - 检查是否设置了错误信息 - >
**< span class =errorid =fullname><?php echo isset($ error ['fullname'])?$ error ['fullname']:''; ?>< / span> **

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

< input type =passwordid =passwordname =password/>
**< span class =errorid =password><?php echo isset($ error ['password'])?$ error ['password']:''; ?>< / span> **

< input type =passwordid =password2name =password2/>
**< span class =errorid =divCheckPasswordMatch><?php echo isset($ error ['password2'])?$ error ['password2']:''; ?>< / span> **

< input type =emailid =emailname =email/>
**< span class =errorid =email><?php echo isset($ error ['email'])?$ error ['email']:''; ?>< / span> **

< p class =submit>
< button type =submitid =submitname =submitvalue =Register>注册< / button>
< / p>

< / form>



注:



请不要在新代码中使用 mysql_ * 函数 。他们不再维护并被正式弃用。请参阅 红色框 ?了解 准备语句 ,并使用 PDO MySQLi - 这篇文章将帮助你决定哪个。如果您选择PDO,这里是一个很好的教程


Continuing from my previous post "PHP and MYSQL database connection and table creation only once", I created the registration form with the PHP code and server side validation. I’m getting some errors as stated below. i.e. all errors are occurring at the place where i try to print the errors in their respected html class "". I've made the html "span class" text bold for easy recognition. If their is anything extra solutions for better performance of the form please let me know...

List of errors:

Notice: Undefined variable: error_name in C:\wamp\www\18+\register.php
Notice: Undefined variable: error_username in C:\wamp\www\18+\register.php
Notice: Undefined variable: error_password in C:\wamp\www\18+\register.php
Notice: Undefined variable: error_password2 in C:\wamp\www\18+\register.php
Notice: Undefined variable: error_email in C:\wamp\www\18+\register.php

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>

解决方案

The problem is, you are setting the messages only if certain conditions are true. Thus, these variables are not found if those conditions aren't true. To resolve this, use isset() when displaying the errors, e.g.:

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

This means check if $error_name is set, if yes display it or display nothing.

Another thing (logically) is that your code is not actually checking for the errors. The $error remains an empty string and you are checking if it is empty which will always be true. You need to either store the errors as arrays or check if all the variables are empty.

Additional:

Can u tell me how to store the errors as arrays..plz

Try this:

<?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...!';
        }
    }   
} 
?>

Change your HTML to:

<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>

Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

这篇关于未定义的变量在PHP注册从的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆