一个设计 - 注册界面(一)
这里用的是MySQL+PHP基友组合
首先,我们需要创建一个数据库,并且创建user(id,name,pw)方便起见,我们将id设置为主键(默认)+自动增长
这里使用mysqli_connect,注意和mysql区别一下,确定连接成功之后,我们判断输入密码是否一致
$link = mysqli_connect('localhost', 'root', '你的数据库密码', '数据库名字');
1 <?php 2 $link = mysqli_connect('localhost', 'root', '你的数据库密码', '数据库名字'); 3 if (!$link) { 4 die('Could not connect: ' . mysql_error()); 5 }else { 6 if (isset($_POST['submit'])){ 7 if ($_POST['pw'] == $_POST['repw']){ 8 //下面有说 9 }else { 10 echo "<script>alert('两次输入密码不一致!')</script>"; 11 } 12 } 13 } 14 ?>
判断用户名是否存在,mysqli_num_rows可以获取该用户名是否存在
echo当然是调试用的(不用管啦)
1 $query = "select * from user where name = '{$_POST['name']}' "; 2 $result=mysqli_query($link, $query); 3 $num=mysqli_num_rows($result); 4 if($num){ 5 echo $num;//echo "<script>alert('存在!')</script>"; 6 }else{ 7 $query1 = "insert into user (name,pw) values('{$_POST['name']}','{$_POST['pw']}')"; 8 $result1=mysqli_query($link, $query1); 9 echo $result1; 10 }
把注册界面放进去
1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>无标题文档</title> 6 <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"> 7 <style> 8 body{ 9 background: black; 10 } 11 </style> 12 </head> 13 <body> 14 <div style="width: 100px;height: 100px;background: url(../img/bg/reg.png);margin: 40px auto;"> 15 16 </div> 17 <form method="post" style="width: 300px;height: 390px;border: 1px solid white;margin: 0px auto;" action="存放php的地址"> 18 <div style="width: 100px;height: 40px;margin-left: 40px;margin-top: 50px;"> 19 <p style="color: white;text-align: center;">用户名 20 </p> 21 </div> 22 <input type="text" name="name" style="margin-left: 70px;"> 23 <div style="width: 100px;height: 40px;margin-left: 40px;margin-top: 50px;"> 24 <p style="color: white;text-align: center;">密码 25 </p> 26 </div> 27 <input type="password" name="pw" style="margin-left: 70px;"> 28 <div style="width: 100px;height: 40px;margin-left: 40px;margin-top: 50px;"> 29 <p style="color: white;text-align: center;margin-left: 30px;">再次确认 30 </p> 31 </div> 32 <input type="password" name="repw" style="margin-left: 70px;"> 33 <br><br> 34 <button type="submit" name="submit" style="margin-left: 125px;">确认</button> 35 </form> 36 </body> 37 </html>
运行一下 输入错误密码 输入存在用户名
1 <?php 2 $link = mysqli_connect('localhost', 'root', '', ''); 3 if (!$link) { 4 die('Could not connect: ' . mysql_error()); 5 }else { 6 if (isset($_POST['submit'])){ 7 if ($_POST['pw'] == $_POST['repw']){ 8 $query = "select * from user where name = '{$_POST['name']}' "; 9 $result=mysqli_query($link, $query); 10 if($result){ 11 echo "<script>alert('存在!')</script>"; 12 }else{ 13 $Requery = "insert into user (name,pw) values('{$_POST['name']}','{$_POST['pw']}')"; 14 } 15 }else { 16 echo "<script>alert('两次输入密码不一致!')</script>"; 17 } 18 } 19 } 20 ?>