先來截圖下需要用到的文件
這個是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>
這些代碼是從別人博客看的之后修改了一下 還有很多地方沒做好 注冊沒有寫規則處理...
學海無邊,書囊無底
有什么錯的地方 請大佬們指點