1掏湾、注冊頁面和登陸頁面差不多,寫了登陸后注冊也就多加了一條mysql添加一句她君。先看一下界面樣子
101.PNG
這是上次寫的登陸頁面點注冊就會轉(zhuǎn)跳到 注冊頁面
102.PNG
在這里我只設置了兩個判斷(注冊頁面所有選項不能位空,必填)(用戶名不能用已存在的)
104.PNG
103.PNG
下面是頁面的代碼
<!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>
- 這里判斷的方法是看提交按鈕是否被點擊了 ,如果被提交過來再判斷提交的內(nèi)容是否有空值姜性。(其實這里還可以加上很多過濾的函數(shù)以防止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'
這是寫入,把數(shù)據(jù)寫入user中污抬。
INSERT INTO user (username,email,sex,password) VALUES('$name','$email','$sex','$password')