php用户注册表单提交的实例介绍

发布时间:2020-01-26编辑:脚本学堂
本文介绍下,用php实现一个用户注册表单提交的例子,有需要的朋友,参考下吧。

通过以下的例子,大家可以掌握使用php创建一个简单的用户注册表单及提交程序的方法。

1,php代码部分

<?php
/**
* 用户注册
* edit www.jb200.com
*/
if(isset($_POST['submit'])){
    # connect to the database here
    # search the database to see if the user name has been taken or not
    $query = sprintf("SELECT * FROM users WHERE user_name='%s' LIMIT 1",mysql_real_escape_string($_POST['user_name']));
    $sql = mysql_query($query);
    $row = mysql_fetch_array($sql);
    #check too see what fields have been left empty, and if the passwords match
    if($row||empty($_POST['user_name'])|| empty($_POST['fname'])||empty($_POST['lname'])|| empty($_POST['email'])||empty($_POST['password'])|| 
    empty($_POST['re_password'])||$_POST['password']!=$_POST['re_password']){
        # if a field is empty, or the passwords don't match make a message
        $error = '<p>';
        if(empty($_POST['user_name'])){
            $error .= 'User Name can't be empty<br>';
        }
        if(empty($_POST['fname'])){
            $error .= 'First Name can't be empty<br>';
        }
        if(empty($_POST['lname'])){
            $error .= 'Last Name can't be empty<br>';
        }
        if(empty($_POST['email'])){
            $error .= 'Email can't be empty<br>';
        }
        if(empty($_POST['password'])){
            $error .= 'Password can't be empty<br>';
        }
        if(empty($_POST['re_password'])){
            $error .= 'You must re-type your password<br>';
        }
        if($_POST['password']!=$_POST['re_password']){
            $error .= 'Passwords don't match<br>';
        }
        if($row){
            $error .= 'User Name already exists<br>';
        }
        $error .= '</p>'
    }else{
        # If all fields are not empty, and the passwords match,
        # create a session, and session variables,
        $query = sprintf("INSERT INTO users_table(`user_name`,`f_name`,`l_name`,`email`,`password`)
            VALUES('%s','%s','%s','%s',PASSWORD('%s'))",
            mysql_real_escape_string($_POST['user_name']),
            mysql_real_escape_string($_POST['fname']),
            mysql_real_escape_string($_POST['lname']),
            mysql_real_escape_string($_POST['email']),
            mysql_real_escape_string($_POST['password']))or die(mysql_error());
        $sql = mysql_query($query);
        # Redirect the user to a login page
        header("Location: login.php");
        exit;
    }
}
# echo out each variable that was set from above,
# then destroy the variable.
if(isset($error)){
    echo $error;
    unset($error);
}
?>

2,html内容部分

<form action=" <? echo $_SERVER['PHP_SELF']; ?> " method="post">
    <p>User Name:<br /><input type="text" name="user_name"  <? if(!$row){echo 'value="'.$_POST['user_name'].'"';} ?> /></p>
    <p>First Name:<br /><input type="text" name="fname"  <? echo 'value="'.$_POST['fname'].'"'; ?> /></p>
    <p>Last Name:<br /><input type="text" name="lname"  <? echo 'value="'.$_POST['lname'].'"'; ?> /></p>
    <p>Email:<br /><input type="text" name="email"  <? echo 'value="'.$_POST['email'].'"'; ?> /></p>
    <p>Password:<br /><input type="password" name="password" /></p>
    <p>Re-Type Password:<br /><input type="password" name="re_password" /></p>
    <p><input type="submit" name="submit" value="Sign Up" /></p>
</form>

很简单,适合初学php的朋友参考,建议大家自行练习一下。