要求
创建数据库“zhuang”
创建表“user”,ID自动递增
实现:
1、用户名、邮箱、密码不为空
2、两次密码输入一致
3、查询用户是否被注册过
效果图
index.html代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>注册新用户</title>
<style>
body {
background-color: #dedede;
}
.main {
border: 7px dotted #00bcff;
width: 999px;
margin: 222px auto;
padding: 28px;
font-size: 27px;
}
h1 {
text-align: center;
font-size: 40px;
}
table {
margin: 0 auto;
}
td {
text-align: right;
}
input {
height: 30px;
}
button {
width: 100px;
height: 40px;
font-size: 16px;
margin-top: 30px;
margin-left: 66px;
background-color: #4BB2FF;
color: #FFC633;
border: none;
border-radius: 28px;
}
button:hover{
color: #fff;
background-color: #FF794E;
}
</style>
</head>
<body>
<div class="main">
<h1>欢迎注册新用户</h1>
<form action="zhuce.php" method="POST" name="zhuce">
<table>
<tr>
<td>用 户 名:</td>
<td><input type="text" name="user" id="user" /></td>
</tr>
<tr>
<td>邮 箱:</td>
<td><input type="email" name="email" id="email" /></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="psd" id="psd" /></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" id="psd2" " /></td>
</tr>
<tr>
<td colspan=" 2">
<button type="submit" onclick="return bj()" />提交注册</button>
<button type="reset" />重新填写</button>
</td>
</tr>
</table>
</form>
</div>
<script>
function bj() {
var user = document.getElementById("user").value;
var email = document.getElementById("email").value;
var psd = document.getElementById("psd").value;
var psd2 = document.getElementById("psd2").value;
if (user == "") {
alert("用户名不能为空,请重新输入");
return false;
}
if (email == "") {
alert("邮箱不能为空,请重新输入");
return false;
}
if (psd == "") {
alert("密码不能为空,请重新输入");
return false;
}
if (psd2 == "") {
alert("确认密码不能为空,请重新输入");
return false;
}
if (psd != psd2) {
alert("两次密码输入不一致,请重新输入");
return false;
}
}
</script>
</body>
</html>
zhuce.php代码
<?php
$username = $_POST['user'];
$password = $_POST['psd'];
$email = $_POST['email'];
$ip = $_SERVER['REMOTE_ADDR'];
$link = mysqli_connect("localhost", "root", "root");
if (!$link) {
echo die("数据库链接失败") . mysqli_error();
} else {
//选择数据库
mysqli_query($link, "use zhuang");
//选择表
//判断用户名是否存在
$sql = "select `id` from `user` where `username`='$username'";
$rst = mysqli_query($link, $sql);
if (mysqli_fetch_row($rst)) {
echo "<script language=\"JavaScript\">\r\n";
echo " alert(\"用户名已经存在,请换个用户名。\");\r\n";
echo " history.back();\r\n";
echo "</script>";
exit;
// exit('用户名已经存在,请换个用户名。');
}
//拼接SQL语句
$sql = "insert into `user` (`username`,`password`,`email`) values ('$username','$password','$email')";
//执行SQL语句
$rst = mysqli_query($link, $sql);
//输出执行的SQL语句和执行结果,便于调试程序
// echo "SQL语句:$sql<br>";
$zhuce = "账号:$username ,密码:$password ,邮箱$email ,IP:$ip";
echo $zhuce;
if ($rst) {
echo "<script language=\"JavaScript\">\r\n";
echo " alert(\"注册成功。\");\r\n";
echo "</script>";
exit;
// echo '执行成功';
} else {
echo '执行失败:' . mysqli_error($link);
}
}
?>
data.sql代码
create table `user` (
`id` int unsigned primary key auto_increment,
`username` varchar(10) not null unique,
`password` char(32) not null,
`email` varchar(40) not null
)charset=utf8;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/83727.html