类别:PHP问题 / 日期:2019-12-02 / 浏览:92 / 评论:0

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>

2、编写背景注册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"); //预备SQL语句,查询用户名
    $sql_select = "SELECT username FROM usertext WHERE username = '$username'"; //实行SQL语句
    $ret = mysqli_query($conn, $sql_select);
    $row = mysqli_fetch_array($ret); //推断用户名是不是已存在
    if ($username == $row['username']) { //用户名已存在,显现提醒信息
        header("Location:register.php?err=1");
    } else { //用户名不存在,插进去数据 //预备SQL语句
        $sql_insert = "INSERT INTO usertext(username,password,sex,qq,email,phone,address) 
VALUES('$username','$password','$sex','$qq','$email','$phone','$address')"; //实行SQL语句
        mysqli_query($conn, $sql_insert);
        header("Location:register.php?err=3");
    } //封闭数据库
    mysqli_close($conn);
} else {
    header("Location:register.php?err=2");
} ?>

3、数据表设想:

4、结果:

更多PHP相干学问,请接见ki4网!

以上就是php怎样完成注册的细致内容,更多请关注ki4网别的相干文章!

打赏

感谢您的赞助~

打开支付宝扫一扫,即可进行扫码打赏哦~

版权声明 : 本文未使用任何知识共享协议授权,您可以任何形式自由转载或使用。

 可能感兴趣的文章

评论区

发表评论 / 取消回复

必填

选填

选填

◎欢迎讨论,请在这里发表您的看法及观点。