PHP開發留言板教學之註冊功能
看下面的一段程式碼
<!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>註冊頁面是提交到regin.php ,下面我們來分析一下
連結資料庫,引入conn.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);
#然後我們要對$res 判斷,若為真即為資料庫有這些資訊,提示用戶已被註冊。為假,我們可以註冊,把獲取的資訊加入資料庫
程式碼如下:
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){## );
if($result){## );
if($result){## /clt); .href='message.php';</script>";
}else{
echo "<); ;/script>";
}
reg.php 完整程式碼如下:
<?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>";
}
}
?>######reg.php 完整程式碼如下:###rrreee##########
- 課程推薦
- 課件下載
課件暫不提供下載,工作人員正在整理中,後期請多關注該課程~ 















