php教程-案例16(用户注册)

<?php
//case 16 用户注册

/*
 * 建表SQL语句
 *
 *create table emp_user(
    id int unsigned auto_increment primary key,
    user_name varchar(10) not null unique,
    password char(32) not null,
    email varchar(40) not null
)charset=utf8;
 *
 *
 *
 */


?>

<html>
<head>
    <title>用户注册</title>
    <script type="text/javascript">
        function checkInput(form) {
            var user_name;
            user_name = form.user_name;
            if(user_name.value == '')
            {
                alert('用户名不能为空!');
                return false;
            }

            var email = document.getElementById('email');
            if(email.value == '')
            {
                alert('邮箱不能为空!');
                return false;
            }

            if(document.forms[0].password.value == '')
            {
                alert('密码不能为空');
                return false;
            }

            var pwd = form.password;
            var pwd_2 = document.getElementById('password_2');
            if(pwd.value != pwd_2.value)
            {
                alert('密码不一致!');
                return false;
            }
            return true;
        }
    </script>
</head>
<body>
<form name="form1" method="post" action="./t206.php" onsubmit="return checkInput(this);">
    <h2>注册新用户</h2>
    <p>用户名:<input type="text" name="user_name" id="user_name"></p>
    <p>邮箱:<input type="text" name="email" id="email"></p>
    <p>密码:<input type="password" name="password" id="password"></p>
    <p>确认密码:<input type="password" name="password_2" id="password_2"></p>
    <p><input type="submit" name="register" id="register" value="注册" style="width: 150px;"></p>
    <p><input type="reset" value="重新填写" style="width: 150px;"></p>
</form>
</body>
</html>

<?php
header('Content-type:text/html;charset=utf-8');
//判断点击注册按钮
if(isset($_POST['register']))
{
    echo "<hr>";
    $user_name = $_POST['user_name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    //连接数据库
    $connect = mysqli_connect('localhost','root','mysql123','test');
    if(!$connect)
    {
        die('数据库连接失败!').mysqli_error();
    }
    //设置字符集
    mysqli_query($connect,"set names utf8");
    //防止SQL注入
    $user_name = mysqli_real_escape_string($connect,$user_name);
    $email = mysqli_real_escape_string($connect,$email);
    //查询重名用户
    $sql = "select id from emp_user where user_name='$user_name'";
    //执行查询
    $res = mysqli_query($connect,$sql);
    //取数据
    $row = mysqli_fetch_row($res);
    //print_r($row);
    if($row)
    {
        die('用户名已经存在,请重新注册!');
    }
    //密码加密
    $password = md5($password);
    //插入语句
    $sql_insert = "insert into emp_user(user_name,password,email) values('$user_name','$password','$email')";
    //保存
    $res = mysqli_query($connect,$sql_insert);
    //保存成功
    if($res > 0)
    {
        echo "<script>alert('注册成功!');</script>";
        echo "<h2>接收到新用户注册!</h2>";
        echo "<p>用户名:$_POST[user_name]</p>";
        echo "<p>密码:".$_POST['password']."</p>";
        echo "<p>邮箱:$_POST[email]</p>";
        echo "<p>IP地址:$_SERVER[REMOTE_ADDR]</p>";
        echo "<p>浏览器环境:".$_SERVER['HTTP_USER_AGENT']."</p>";
        echo '<p>请求来源:'.$_SERVER['HTTP_REFERER'].'</p>';
    }

}

?>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值