PHP開發(fā)之注冊后臺思路及代碼
前端所有都準(zhǔn)備和制作好了,下面我們就用PHP來實現(xiàn)這些功能。
首先我們來整理下思路,我們通過下面的流程圖可以清晰的看到我們的整體思路:
<?php header("content-type:text/html;charset=utf-8"); //連接數(shù)據(jù)庫 $link = mysqli_connect("localhost","root","root","regedit"); if (!$link) { die("連接失敗: " . mysqli_connect_error()); } $username=$_POST['username']; $password=$_POST['password']; $pwd_again=$_POST['pwd_again']; if($username == "" || $password == "" || $pwd_again == "") { echo "請確認(rèn)信息完整性"; }else{ if($password!=$pwd_again) { echo"兩次輸入的密碼不一致,請重新輸入!"."<br/><br/>"; echo"<a href='regedit.html'>重新輸入</a>"; } else { $sql="insert into form(username,password) values('$username','$password')"; $result=mysqli_query($link,$sql); if(!$result) // 判斷數(shù)據(jù)是否成功插入進(jìn)數(shù)據(jù)庫 { echo"注冊不成功!"."<br/><br/>"; echo"<a href='regedit.html'>返回</a>"; } else { echo"注冊成功!"."<br/><br/>"; echo"<a href='login.html'>立刻登錄</a>"; } } } ?>
首先,因為要將數(shù)據(jù)傳入數(shù)據(jù)庫中,所以先要連接數(shù)據(jù)庫
然后對注冊時填寫的數(shù)據(jù)進(jìn)行驗證,判斷兩次密碼是不是一樣,且是不是注冊信息填寫完整,不完整或不一樣的話注冊失敗。
最后用數(shù)據(jù)庫的插入語句將注冊內(nèi)容添加到數(shù)據(jù)庫中,判斷是否添加成功。添加失敗的話則注冊失敗,否則就是成功。