1、注册页面和登陆页面差不多,写了登陆后注册也就多加了一条mysql添加一句。先看一下界面样子
这是上次写的登陆页面点注册就会转跳到 注册页面
在这里我只设置了两个判断(注册页面所有选项不能位空,必填)(用户名不能用已存在的)
下面是页面的代码
<!DOCTYPE html>
<html>
<head>
<title>注册</title>
<meta charset="utf-8">
</style>
</head>
<body background="3.jpg" >
<?php
$password=$nameeor=$emaileor=$sexeor=$passwordeor=$password2eor="";
if (empty($_POST["submit"])) {
}else{
if (empty($_POST["username"])){
$nameeor= "名字不能为空";
}else{
$name=$_POST["username"];
}
if(empty($_POST["email"])){
$emaileor= "email 不能为空";
}else{
$email=$_POST["email"];
}
if(empty($_POST["sex"])){
$sexeor= "Sex 不能为空";
}else{
$sex=$_POST["sex"];
}
if(empty($_POST["password1"])){
$passwordeor= "password 不能为空";
}else{
$password = $_POST["password1"];
}
if($password == $_POST["password2"]){
}
else{
$password2eor="两次密码不一样";
}
if (isset($name,$email,$sex,$password)) {
$servername = "localhost";
$dbname = "root";
$dbpassword = "root";
$db = "shiyan";
$conn = mysqli_connect($servername,$dbname,$dbpassword,$db);
if (!isset($conn))
{
die("连接失败:". mysqli_error());
}
$sql="SELECT * from user where username = '$name'";
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0)
{
echo "用户名已存在!";
}
else
{
$sql = "INSERT INTO user (username,email,sex,password) VALUES('$name','$email','$sex','$password')";
if($conn->query($sql) === TRUE)
{
echo "<a href='denglu.php'>注册成功,点击登陆!!</a>";
}
else
{
echo "注册失败!";
}
}
$conn -> close();
}
}
?>
<form method="post">
<center>
<table >
<tr style="height:160px">
<td colspan="2" align="center" >
<font size="12" >注册界面</font>
</td>
</tr>
<tr>
<td >
<font size="6">用户名: </font>
</td>
<td>
<input type="text" name="username" style="width:250px;height:30px">
<font size="5" color="red"><?php echo "$nameeor";?> </font>
</td>
</tr>
<tr>
<td>
<font size="6"> email:</font>
</td>
<td>
<input type="text" name="email" style="width:250px;height:30px">
<font size="5" color="red"><?php echo "$emaileor";?> </font>
</td>
</tr>
<tr>
<td colspan="2">
<font size="6"> Sex : </font>
<input type="radio" name="sex" value="male" checked>
<font size="6"> 男:</font>
<input type="radio" name="sex" value="female">
<font size="6"> 女:</font>
<font size="5" color="red"><?php echo "$sexeor";?></font>
</td>
</tr>
<td>
<font size="6"> 密 码:</font>
</td>
<td>
<input type="password" name="password1" style="width:250px;height:30px">
<font size="5" color="red"><?php echo "$passwordeor";?> </font>
</td>
</tr>
<tr>
<td>
<font size="6"> 确认密码:</font></td>
<td>
<input type="password" name="password2" style="width:250px;height:30px">
<font size="5" color="red"><?php echo "$password2eor";?> </font>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="提交" style="width:125px;height:30px">
<input type="reset" name="reset" value="重置" style="width:125px;height:30px">
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
- 这里判断的方法是看提交按钮是否被点击了 ,如果被提交过来再判断提交的内容是否有空值。(其实这里还可以加上很多过滤的函数以防止sql注入)
<?php
$password=$nameeor=$emaileor=$sexeor=$passwordeor=$password2eor="";
if (empty($_POST["submit"])) {
}else{
if (empty($_POST["username"])){
$nameeor= "名字不能为空";
}else{
$name=$_POST["username"];
}
if(empty($_POST["email"])){
$emaileor= "email 不能为空";
}else{
$email=$_POST["email"];
}
if(empty($_POST["sex"])){
$sexeor= "Sex 不能为空";
}else{
$sex=$_POST["sex"];
}
if(empty($_POST["password1"])){
$passwordeor= "password 不能为空";
}else{
$password = $_POST["password1"];
}
if($password == $_POST["password2"]){
}
else{
$password2eor="两次密码不一样";
}
?>
注册页面所需要的sql语句
判断是否user表是否有这个用户名,有就要从新输入
SELECT * from user where username = '$name'
这是写入,把数据写入user中。
INSERT INTO user (username,email,sex,password) VALUES('$name','$email','$sex','$password')