您的位置:首页 > 其它

一个设计 - 注册界面(一)

2017-09-18 00:27 190 查看
这里用的是MySQL+PHP基友组合

首先,我们需要创建一个数据库,并且创建user(id,name,pw)方便起见,我们将id设置为主键(默认)+自动增长





这里使用mysqli_connect,注意和mysql区别一下,确定连接成功之后,我们判断输入密码是否一致

$link = mysqli_connect('localhost', 'root', '你的数据库密码', '数据库名字');


<?php
$link = mysqli_connect('localhost', 'root', '你的数据库密码', '数据库名字');
if (!$link) {
die('Could not connect: ' . mysql_error());
}else {
if (isset($_POST['submit'])){
if ($_POST['pw'] == $_POST['repw']){
//下面有说
}else {
echo "<script>alert('两次输入密码不一致!')</script>";
}
}
}
?>


判断用户名是否存在,mysqli_num_rows可以获取该用户名是否存在

echo当然是调试用的(不用管啦)


$query = "select * from user where name = '{$_POST['name']}' ";
$result=mysqli_query($link, $query);
$num=mysqli_num_rows($result);
if($num){
echo $num;//echo "<script>alert('存在!')</script>";
}else{
$query1 = "insert into user (name,pw) values('{$_POST['name']}','{$_POST['pw']}')";
$result1=mysqli_query($link, $query1);
echo $result1;
}


把注册界面放进去

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<style>
body{
background: black;
}
</style>
</head>
<body>
<div style="width: 100px;height: 100px;background: url(../img/bg/reg.png);margin: 40px auto;">

</div>
<form method="post" style="width: 300px;height: 390px;border: 1px solid white;margin: 0px auto;" action="存放php的地址">
<div style="width: 100px;height: 40px;margin-left: 40px;margin-top: 50px;">
<p style="color: white;text-align: center;">用户名
</p>
</div>
<input type="text" name="name" style="margin-left: 70px;">
<div style="width: 100px;height: 40px;margin-left: 40px;margin-top: 50px;">
<p style="color: white;text-align: center;">密码
</p>
</div>
<input type="password" name="pw" style="margin-left: 70px;">
<div style="width: 100px;height: 40px;margin-left: 40px;margin-top: 50px;">
<p style="color: white;text-align: center;margin-left: 30px;">再次确认
</p>
</div>
<input type="password" name="repw" style="margin-left: 70px;">
<br><br>
<button type="submit" name="submit" style="margin-left: 125px;">确认</button>
</form>
</body>
</html>


运行一下 输入错误密码 输入存在用户名







<?php
$link = mysqli_connect('localhost', 'root', '', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}else {
if (isset($_POST['submit'])){
if ($_POST['pw'] == $_POST['repw']){
$query = "select * from user where name = '{$_POST['name']}' ";
$result=mysqli_query($link, $query);
if($result){
echo "<script>alert('存在!')</script>";
}else{
$Requery = "insert into user (name,pw) values('{$_POST['name']}','{$_POST['pw']}')";
}
}else {
echo "<script>alert('两次输入密码不一致!')</script>";
}
}
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: