您好,登錄后才能下訂單哦!
這篇文章運(yùn)用簡(jiǎn)單易懂的例子給大家介紹如何在php中實(shí)現(xiàn)用戶注冊(cè),代碼非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
1、編寫前端注冊(cè)form表單代碼
<!DOCTYPE html> <html> <head><title>注冊(cè)</title> <meta name="content-type"; charset="UTF-8"> </head><body> <div class="content" align="center"> <!--頭部--> <div class="header"> <h2>注冊(cè)頁(yè)面</h2> </div> <!--中部--> <div class="middle"> <form action="registeraction.php" method="post"> <table border="0"> <tr> <td>用戶名:</td> <td><input type="text" id="id_name" name="username" required="required"></td> </tr> <tr> <td>密 碼:</td> <td><input type="password" id="password" name="password" required="required"></td> </tr> <tr> <td>重復(fù)密碼:</td> <td><input type="password" id="re_password" name="re_password" required="required"></td> </tr> <tr> <td>性別:</td> <td> <input type="radio" id="sex" name="sex" value="mam">男 <input type="radio" id="sex" name="sex" value="woman">女 </td> </tr> <tr> <td>QQ:</td> <td><input type="text" id="qq" name="qq" required="required"></td> </tr> <tr> <td>Email:</td> <td><input type="email" id="email" name="email" required="required"></td> </tr> <tr> <td>電話:</td> <td><input type="text" id="phone" name="phone" required="required"></td> </tr> <tr> <td>地址:</td> <td><input type="text" id="address" name="address" required="required"></td> </tr> <tr> <td colspan="2" align="center" style="color:red;font-size:10px;"> <!--提示信息--> <?php $err = isset($_GET["err"]) ? $_GET["err"] : ""; switch ($err) { case 1: echo "用戶名已存在!"; break; case 2: echo "密碼與重復(fù)密碼不一致!"; break; case 3: echo "注冊(cè)成功!"; break; } ?> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" id="register" name="register" value="注冊(cè)"> <input type="reset" id="reset" name="reset" value="重置"> </td></tr> <tr> <td colspan="2" align="center"> 如果已有賬號(hào),快去<a href="login.php">登錄</a>吧! </td> </tr> </table> </form> </div> <!--腳部--> <div class="footer"> <small>Copyright © 版權(quán)所有·歡迎翻版 </div> </div></body></html>
2、編寫后臺(tái)注冊(cè)registeraction.php代碼
<?php // $Id:$ //聲明變量 $username = isset($_POST['username']) ? $_POST['username'] : ""; $password = isset($_POST['password']) ? $_POST['password'] : ""; $re_password = isset($_POST['re_password']) ? $_POST['re_password'] : ""; $sex = isset($_POST['sex']) ? $_POST['sex'] : ""; $qq = isset($_POST['qq']) ? $_POST['qq'] : ""; $email = isset($_POST['email']) ? $_POST['email'] : ""; $phone = isset($_POST['phone']) ? $_POST['phone'] : ""; $address = isset($_POST['address']) ? $_POST['address'] : ""; if ($password == $re_password) { //建立連接 $conn = mysqli_connect("localhost", "", "", "user"); //準(zhǔn)備SQL語(yǔ)句,查詢用戶名 $sql_select = "SELECT username FROM usertext WHERE username = '$username'"; //執(zhí)行SQL語(yǔ)句 $ret = mysqli_query($conn, $sql_select); $row = mysqli_fetch_array($ret); //判斷用戶名是否已存在 if ($username == $row['username']) { //用戶名已存在,顯示提示信息 header("Location:register.php?err=1"); } else { //用戶名不存在,插入數(shù)據(jù) //準(zhǔn)備SQL語(yǔ)句 $sql_insert = "INSERT INTO usertext(username,password,sex,qq,email,phone,address) VALUES('$username','$password','$sex','$qq','$email','$phone','$address')"; //執(zhí)行SQL語(yǔ)句 mysqli_query($conn, $sql_insert); header("Location:register.php?err=3"); } //關(guān)閉數(shù)據(jù)庫(kù) mysqli_close($conn); } else { header("Location:register.php?err=2"); } ?>
3、數(shù)據(jù)表設(shè)計(jì):
4、效果:
關(guān)于如何在php中實(shí)現(xiàn)用戶注冊(cè)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。