摘要:HTML代码<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8">
HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<p><label for="email">邮箱:</label><input type="email" id="email" name="email"></p>
<p><label for="password">密码:</label><input type="password" id="password" name="password"></p>
<p><button>注册</button></p>
<script type="text/javascript" src="static/jquery-3.3.1.js"></script>
<script>
$('button').click(function(){
if ($('#email').val().length === 0) {
$('#email').after('<span style="color:red">邮箱不能为空</span>').next().fadeOut(2000);
$('#email').focus();
return false;
}
if ($('#password').val().length === 0) {
$('#password').after('<span style="color:red">密码不能为空</span>').next().fadeOut(2000);
$('#password').focus();
return false;
} else if($('#password').val().length < 6) {
$('#password').after('<span style="color:red">密码不能少于6位</span>').next().fadeOut(2000);
$('#password').focus();
return false;
}
$.ajax({
type: 'post', // 请求类型
url: 'check.php', // 请求的处理脚本
data: {
email: $('#email').val(),
password: $('#password').val()
},
dataType: 'json',
success: function(data,status,xhr) {
console.log($(this));
// console.log(data.message);
if (data.status === 1) {
$('#email').after('<span style="color: red"></span>').next().html(data.message).fadeOut(2000);
}else {
$('button').after('<span style="color: green"></span>').next().html(data.message).fadeOut(2000);
}
}
});
})
</script>
</body>
</html>PHP代码
<?php
//连接数据库
$db=@mysqli_connect('127.0.0.1','root','root','phpcn',3306);
if(!$db){
exit('数据库连接错误:'.mysqli_connect_error());
}
$email = htmlspecialchars(trim($_POST['email']));
$password = htmlspecialchars(trim($_POST['password']));
$res=count_number($db,'admin','email',"$email");
if ($res) {
$status = 1;
$message = '该邮箱已注册!';
}else{
$sql="INSERT INTO `admin` (email,password) VALUES ('{$email}','{$password}')";
$return=mysqli_query($db,$sql);
if($return){
$status = 0;
$message = '注册成功!';
}else{
$status=0;
$message ='注册失败!';
}
}
mysqli_close($db);
echo json_encode(['status' => $status, 'message' => $message]);
function count_number($db,$table,$value,$key){
$sql="SELECT COUNT(*) AS count_number FROM ".$table." WHERE ".$value.'="'.$key.'"';
$return=mysqli_query($db,$sql);
$return=mysqli_fetch_assoc($return);
return $return['count_number'];
}
批改老师:查无此人批改时间:2019-05-05 09:08:12
老师总结:完成的不错。以后都用pdo进行查询了,但是也要了解下mysqli。继续加油。
