Look at the code below
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>注册</title> <style type="text/css"> *{margin: 0px;padding: 0px;} body{ background:#eee;} #div{width:300px;height:400px; background:#B1FEF9;margin:0 auto;margin-top:150px; border-radius:20px;} h3{margin-left:48px;padding-top:60px;} h4{margin-left:120px;padding-top:60px;font-size: 18px;} #cnt{width:280px;height:370px;margin-left:33px;padding-top:60px;} .sub1{ width:70px;height:30px;border:1px solid #fff; background:#eee;margin-left:150px;margin-top:20px;} </style> </head> <body> <div id="div"> <h4>会员注册</h4> <div id="cnt"> <form method="post" action="regin.php"> 用户名:<input type="text" placeholder="请输入用户名" name="username"> <br><br> 密 码:<input type="password" placeholder="请输入密码" name="password"> <br><br> <input type="submit" value="注册" class="sub1"> </form> </div> </div> </body> </html>
The registration page is submitted to regin.php, let’s analyze it
Link the database and introduce the file conn.php
require_once('conn.php');//Introducing the connection database file
When we write the registration, if the form submission information already exists in the database, it should not be allowed to register, for example: the database already has With the user "Zhang San", it is not advisable to use "Zhang San" when registering, so we first need to obtain the information submitted by the form, and then go to the database to query whether the information exists. The code is as follows:
$name = $_POST['username'];
$pwd = md5($_POST['password']);
$sql = "select * from user where username='$name'" ;
$info = mysql_query($sql);
$res = mysql_num_rows($info);
Then we have to judge $res. If it is true, the information exists in the database. Prompts that the user has been registered. is false, we can register and add the obtained information to the database
The code is as follows:
if($res){
echo "<script>alert('User already exists , please re-register');location.href='reg.php';</script>";
}else{
$sql1 = "insert into `user` (username,password) values(' $name','$pwd')";
$result = mysql_query($sql1);
if($result){
echo "<script>alert('Registration successful');location .href='message.php';</script>";
}else{
echo "<script>alert('Registration failed');location.href='reg.php';< ;/script>";
}
}
reg.php The complete code is as follows:
<?php require_once('conn.php');//引入连接数据库文件 //注册 $name = $_POST['username']; $pwd = md5($_POST['password']); $sql = "select * from user where username='$name'"; $info = mysql_query($sql); $res = mysql_num_rows($info); if($res){ echo "<script>alert('用户已存在,请重新注册');location.href='reg.php';</script>"; }else{ $sql1 = "insert into `user` (username,password) values('$name','$pwd')"; $result = mysql_query($sql1); if($result){ echo "<script>alert('注册成功');location.href='message.php';</script>"; }else{ echo "<script>alert('注册失败');location.href='reg.php';</script>"; } } ?>