注冊(cè)驗(yàn)證
用戶輸入的用戶名密碼進(jìn)行驗(yàn)證,為空或兩次輸入密碼不一致返回到登錄頁(yè)面(這里也可以自己加寫(xiě)其他驗(yàn)證,如密碼長(zhǎng)度驗(yàn)證),符合驗(yàn)證條件,進(jìn)行數(shù)據(jù)庫(kù)連接,數(shù)據(jù)庫(kù)連接正確如果查詢數(shù)據(jù)庫(kù)用戶名已存在則打印“用戶已存在!”返回登錄頁(yè)面,插入失敗打印“注冊(cè)失??!返回登錄頁(yè)面”;插入成功打印“注冊(cè)成功!返回登錄頁(yè)面“
代碼如下:
add.php
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/2/27 0027 * Time: 上午 11:06 */ header('Content-type:text/html;charset=utf-8'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (empty($_POST['username'])){ echo "<script>alert('用戶名不能為空!');location.href='login.html';</script>"; }else { $username = trim($_POST['username']); } if (empty($_POST['password'])){ echo "<script>alert('密碼不能為空!');location.href='login.html';</script>"; }else{ $password = $_POST['password']; } if (empty($_POST['repassword'])){ echo "<script>alert('確認(rèn)密碼不能為空!');location.href='login.html';</script>"; }else{ $repassword = $_POST['repassword']; } if ($password != $repassword) { echo "<script>alert('兩次輸入密碼不一致!');location.href='login.html';</script>"; } } $mysqli = new mysqli('localhost', 'root', 'root', 'student'); $result = $mysqli->query("SELECT password FROM user WHERE username = "."'$username'"); $rs=$result->fetch_row(); if(!empty($rs)){ echo "<script>alert('用戶已存在!');location.href='login.html';</script>"; }else { $mysqli = new mysqli('localhost', 'root', 'root', 'student'); $sql = "INSERT INTO user (username,password) VALUES ('$_POST[username]', '$_POST[password]')"; $rs = $mysqli->query($sql); if (!$rs) { echo "<script>alert('注冊(cè)失??!');location.href='login.html';</script>"; } else { echo "<script>alert('注冊(cè)成功!返回登錄頁(yè)面');location.href='login.html';</script>"; } }