Rumah > pembangunan bahagian belakang > masalah PHP > php如何实现注册

php如何实现注册

angryTom
Lepaskan: 2023-02-27 13:04:01
asal
2772 orang telah melayarinya

php如何实现注册

php如何实现注册

1、编写前端注册form表单代码

<!DOCTYPE html>
<html>
<head><title>注册</title>
<meta name="content-type"; charset="UTF-8">
</head><body> 
<div class="content" align="center"> <!--头部--> 
<div class="header"> <h1>注册页面</h1> </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>重复密码:</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 "密码与重复密码不一致!";
        break;

    case 3:
        echo "注册成功!";
        break;
}
?> 
</td> </tr> <tr> <td colspan="2" align="center"> 
<input type="submit" id="register" name="register" value="注册">
 <input type="reset" id="reset" name="reset" value="重置"> </td></tr> 
 <tr> <td colspan="2" align="center"> 
如果已有账号,快去<a href="login.php">登录</a>吧! </td> </tr> </table> </form> </div> 
<!--脚部--> 
<div class="footer"> <small>Copyright &copy; 版权所有·欢迎翻版 </div> </div></body></html>
Salin selepas log masuk

2、编写后台注册registeraction.php代码

<?php
// $Id:$ //声明变量
$username = isset($_POST[&#39;username&#39;]) ? $_POST[&#39;username&#39;] : "";
$password = isset($_POST[&#39;password&#39;]) ? $_POST[&#39;password&#39;] : "";
$re_password = isset($_POST[&#39;re_password&#39;]) ? $_POST[&#39;re_password&#39;] : "";
$sex = isset($_POST[&#39;sex&#39;]) ? $_POST[&#39;sex&#39;] : "";
$qq = isset($_POST[&#39;qq&#39;]) ? $_POST[&#39;qq&#39;] : "";
$email = isset($_POST[&#39;email&#39;]) ? $_POST[&#39;email&#39;] : "";
$phone = isset($_POST[&#39;phone&#39;]) ? $_POST[&#39;phone&#39;] : "";
$address = isset($_POST[&#39;address&#39;]) ? $_POST[&#39;address&#39;] : "";
if ($password == $re_password) { //建立连接
    $conn = mysqli_connect("localhost", "", "", "user"); //准备SQL语句,查询用户名
    $sql_select = "SELECT username FROM usertext WHERE username = &#39;$username&#39;"; //执行SQL语句
    $ret = mysqli_query($conn, $sql_select);
    $row = mysqli_fetch_array($ret); //判断用户名是否已存在
    if ($username == $row[&#39;username&#39;]) { //用户名已存在,显示提示信息
        header("Location:register.php?err=1");
    } else { //用户名不存在,插入数据 //准备SQL语句
        $sql_insert = "INSERT INTO usertext(username,password,sex,qq,email,phone,address) 
VALUES(&#39;$username&#39;,&#39;$password&#39;,&#39;$sex&#39;,&#39;$qq&#39;,&#39;$email&#39;,&#39;$phone&#39;,&#39;$address&#39;)"; //执行SQL语句
        mysqli_query($conn, $sql_insert);
        header("Location:register.php?err=3");
    } //关闭数据库
    mysqli_close($conn);
} else {
    header("Location:register.php?err=2");
} ?>
Salin selepas log masuk

3、数据表设计:

Snipaste_2019-10-19_14-59-19.jpg

 4、效果:

Snipaste_2019-10-19_14-58-39.jpg

更多PHP相关知识,请访问PHP中文网

Atas ialah kandungan terperinci php如何实现注册. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan