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