PHP开发留言板教程之注册功能 登录

PHP开发留言板教程之注册功能

看下面的一段代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>注册</title>
    <style type="text/css">
        *{margin: 0px;padding: 0px;}
        body{
            background:#eee;}
        #div{width:300px;height:400px;
            background:#B1FEF9;margin:0 auto;margin-top:150px;
            border-radius:20px;}
        h3{margin-left:48px;padding-top:60px;}
        h4{margin-left:120px;padding-top:60px;font-size: 18px;}
        #cnt{width:280px;height:370px;margin-left:33px;padding-top:60px;}
        .sub1{
            width:70px;height:30px;border:1px solid #fff;
            background:#eee;margin-left:150px;margin-top:20px;}
    </style>
</head>
<body>
    <div id="div">
        <h4>会员注册</h4>
        <div id="cnt">
            <form method="post" action="regin.php">
                用户名:<input type="text" placeholder="请输入用户名" name="username">
                <br><br>
                密&nbsp;码:<input type="password" placeholder="请输入密码" name="password">
                <br><br>
                <input type="submit" value="注册" class="sub1">
            </form>
        </div>
    </div>
</body>
</html>

注册页面是提交到regin.php ,下面我们来分析一下

链接数据库,引入conn.php的文件

require_once('conn.php');//引入连接数据库文件

我们在写注册的时候,如果数据库已存在表单提交的信息,就应该不让其注册了,例如:数据库中已经有了“张三”这个用户,注册时再使用“张三”,这样是不可取的,所以我们首先要获取表单提交的信息,然后去数据库查询,是否存在该信息,代码如下:

    $name = $_POST['username'];
    $pwd  = md5($_POST['password']);
    $sql = "select * from user where username='$name'";
    $info = mysql_query($sql);
    $res = mysql_num_rows($info);

然后我们要对 $res 进行判断,如果为真即为数据库存在该信息,提示用户已被注册。为假,我们可以注册,把获取的信息添加到数据库

代码如下:

if($res){
        echo "<script>alert('用户已存在,请重新注册');location.href='reg.php';</script>";
    }else{
        $sql1 = "insert into `user` (username,password) values('$name','$pwd')";
        $result = mysql_query($sql1);
        if($result){
            echo "<script>alert('注册成功');location.href='message.php';</script>";
        }else{
            echo "<script>alert('注册失败');location.href='reg.php';</script>";
        }
    }


reg.php 完整代码如下:

<?php
    require_once('conn.php');//引入连接数据库文件
    //注册
    $name = $_POST['username'];
    $pwd  = md5($_POST['password']);

    $sql = "select * from user where username='$name'";
    $info = mysql_query($sql);
    $res = mysql_num_rows($info);
    if($res){
        echo "<script>alert('用户已存在,请重新注册');location.href='reg.php';</script>";
    }else{
        $sql1 = "insert into `user` (username,password) values('$name','$pwd')";
        $result = mysql_query($sql1);
        if($result){
            echo "<script>alert('注册成功');location.href='message.php';</script>";
        }else{
            echo "<script>alert('注册失败');location.href='reg.php';</script>";
        }
    }
?>


下一节
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>注册</title> <style type="text/css"> *{margin: 0px;padding: 0px;} body{ background:#eee;} #div{width:300px;height:400px; background:#B1FEF9;margin:0 auto;margin-top:150px; border-radius:20px;} h3{margin-left:48px;padding-top:60px;} h4{margin-left:120px;padding-top:60px;font-size: 18px;} #cnt{width:280px;height:370px;margin-left:33px;padding-top:60px;} .sub1{ width:70px;height:30px;border:1px solid #fff; background:#eee;margin-left:150px;margin-top:20px;} </style> </head> <body> <div id="div"> <h4>会员注册</h4> <div id="cnt"> <form method="post" action="regin.php"> 用户名:<input type="text" placeholder="请输入用户名" name="username"> <br><br> 密 码:<input type="password" placeholder="请输入密码" name="password"> <br><br> <input type="submit" value="注册" class="sub1"> </form> </div> </div> </body> </html>
提交 重置代码
章节 评论 笔记 课件
  • 取消 回复 发送
  • 取消 发布笔记 发送