php如何实现注册
1、编写前端注册form表单代码
注册注册页面
用户名: | |
密 码: | required="required"> |
重复密码: | name="re_password" required="required"> |
性别: | 男 女 |
QQ: | |
Email: | |
电话: | |
地址: | |
$err = isset($_GET["err"]) ? $_GET["err"] : ""; switch ($err) { case 1: echo "用户名已存在!"; break; case 2: echo "密码与重复密码不一致!"; break; case 3: echo "注册成功!"; break; } ?> | |
如果已有账号,快去登录吧! |
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相关知识,请访问PHP中文网!