• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

一个完整的php用户注册表单提交范例

JavaScript 水墨上仙 2163次浏览

这段代码演示了如何通过php建立一个简单的用户注册表单及提交程序

<?php 
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); 
} 
?>  
<!-- Start your HTML/CSS/JavaScript here -->
<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用户注册表单提交范例
喜欢 (0)
加载中……