先来截图下需要用到的文件
这个是signup.html
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>用户注册页面</title> 6 </head> 7 <body> 8 <form action="signup.php" method="post"> 9 <div align="center"> 10 <br> 11 <br> 12 <br> 13 <br> 14 <br> 15 <br> 16 17 <br> 18 <br><br><br> 19 20 21 帐号: <input type="text" name="username"> <br> 22 密码: <input type="password" name="password"> <br> 23 <input type="submit" value="注册" name="submit"> 24 </div> 25 </form> 26 </body> 27 </html>
写好html继续写signup.php 1 <?php
2 header("Content-Type: text/html; charset=utf8"); //设置下UTF-8编码 3 4 if(!isset($_POST['submit'])){ //如果没有检测到提交代码就输出错误
5 echo "运行错误"; 6 } 7 8 $username = $_POST['username']; //传输变量 9 $password = $_POST['password']; 10 11 include ('connect.php'); //连接数据库 12 $_sql = "insert into user (id,username,password) VALUES ('NULL','$username','$password')"; //sql插入数据 自行创建数据表和字段 13 $_result = $_mysqli->query($_sql); //执行$_sql,把结果赋给$_result 14 15 if(!$_result){ //如果没有插入成功就报错 16 echo "Error:" .mysqli_error(); 17 }else{ //否则输出注册成功 18 echo "注册成功"; 19 } 20 21 mysqli_close($_mysqli); //关闭一下数据库 22 ?>
应该先连接数据库的,上代码把 connect.php
<?php $_mysqli = new mysqli('localhost','root','root','register'); //mysqli连接数据库 if (mysqli_connect_errno()){ echo "数据库连接失败:" .mysqli_connect_error(); exit (); } else { echo ""; } ?>
之后就开始登录login.html
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登录中心</title> 6 </head> 7 <body> 8 <form action="login.php" method="post"> 9 <div align="center"> 10 <br> 11 <br> 12 <br> 13 <br> 14 <br> 15 <br> 16 17 <br> 18 <br><br><br> 19 20 21 帐号: <input type="text" name="username"> <br> 22 密码: <input type="text" name="password"> <br> 23 <input type="submit" value="登录" name="submit"> 24 </div> 25 </div> 26 </form> 27 </body> 28 </html>
login.php文件实现功能
1 <?php 2 3 header("Contet-type: text/html; charset=utf8"); 4 5 if(!isset($_POST['submit'])){ 6 echo "运行错误"; 7 } 8 include ('connect.php'); 9 $username = $_POST['username']; 10 $password = $_POST['password']; 11 12 //如果帐号和密码都不是空的 13 if($username && $password) { 14 $_sql = "SELECT * FROM user where username='$username' and password='$password'"; //判断是否和数据库里面的数据一样 15 $_result = $_mysqli->query($_sql); //调用$_sql赋给$_result 16 $_rows = mysqli_num_rows($_result);//调用$_result赋给$_rows 百度看了下 这个 mysqli_num_rows 功能为返回结果集中行的数量 17 18 if($_rows){ 19 header('Location:welcome.php'); //如果正确就跳转到welcome.php 20 }else{ 21 echo "您的用户名或密码错误"; //错误就报错 22 echo " 23 <script> 24 setTimeout(function(){window.location.href='login.html';},3000); //js代码跳转到登录页面 25 </script> 26 27 ";//如果错误使用js 3秒后跳转到登录页面重试; 28 } 29 30 }else{ 31 echo "表单填写不完整"; 32 echo " 33 <script> 34 setTimeout(function(){window.location.href='login.html';},3000); //js代码跳转到登录页面 35 36 </script>"; 37 } 38 39 mysqli_close(); //关闭一下数据库 40 ?>
//这是welcome.php
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登录成功</title> 6 </head> 7 <body> 8 <div align="center"> 9 <h2>登录成功<br> Goodbye</h2> <br> 10 <embed src="http://isure.stream.qqmusic.qq.com/C400004buMEj1X8MxE.m4a?guid=5843645320&vkey=B99212C948A83DB403E2882126ECB016B5C7C6F5D0FBE4A94301B16A2BBDB2657DEE89D504B05E3C57CB73E20AB5CD78C7340CF6CE059333&uin=0&fromtag=66"hidden="true" autostart="true" loop="true"> 11 </div> 12 </body> 13 </html>
这些代码是从别人博客看的之后修改了一下 还有很多地方没做好 注册没有写规则处理...
学海无边,书囊无底
有什么错的地方 请大佬们指点