用戶注冊(cè)對(duì)于初學(xué)php的朋友來說是非常實(shí)用的一個(gè)可以幫助你深入了解數(shù)據(jù)查詢驗(yàn)證與數(shù)據(jù)入庫(kù)及安全的一個(gè)比較經(jīng)典的實(shí)現(xiàn)了,下面一起來看看。
php寫了一個(gè)簡(jiǎn)單的用戶注冊(cè)頁(yè)面。本篇結(jié)合前一篇的內(nèi)容,將注冊(cè)頁(yè)面上提交的信息post 給后面的頁(yè)面register.php ,register.php將post的信息提交入庫(kù)。
一、創(chuàng)建數(shù)據(jù)庫(kù)與表結(jié)構(gòu)
1、建庫(kù)
mysql> create database 361way character set utf8;
Query OK, 1 row affected (0.00 sec)
上面我建了一個(gè)同我站點(diǎn)同命的庫(kù)361way 。
2、創(chuàng)建表結(jié)構(gòu)
CREATE TABLE IF NOT EXISTS `tblmember` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fName` varchar(30) NOT NULL,
`lName` varchar(30) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(60) NOT NULL,
`birthdate` text NOT NULL,
`gender` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
這里的字符編碼我選擇的是utf8 ,并且在該表的id值是從11開始的(前面預(yù)留了10個(gè)),數(shù)據(jù)庫(kù)引擎類型用的InnoDB,具體可以根據(jù)自己的需求修改。
二、post提交php頁(yè)面
向后端提交post請(qǐng)求的register.php代碼如下:
<?php
//set up mysql connection
mysql_connect("localhost", "root", "123456") or die(mysql_error());
//select database
mysql_select_db("361way") or die(mysql_error());
//get the value from the posted data and store it to a respected variable
$fName = $_POST['fName'];
$lName = $_POST['lName'];
$email = $_POST['email'];
$reemail = $_POST['reemail'];
$password = sha1($_POST['password']);
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$gender = $_POST['optionsRadios'];
$birthdate = $year . '-' . $month . '-' . $day;
//insert data using insert into statement
$query = "INSERT INTO tblmember(id, fName, lName, email, password, birthdate, gender)
VALUES (NULL, '{$fName}', '{$lName}', '{$email}', '{$password}', '{$birthdate}', '{$gender}')";
//execute the query
if (mysql_query($query)) {
//dislay a message box that the saving is successfully save
echo "<script type=/"text/javascript/">
alert(/"New member added successfully./");
window.location = /"registration.php/"
</script>";
} else
die("Failed: " . mysql_error());
?>
上面的代碼使用時(shí),數(shù)據(jù)庫(kù)的用戶名密碼及庫(kù)名根據(jù)實(shí)際情況修改。
三、測(cè)試代碼
按上一篇的注冊(cè)頁(yè)面輸入相關(guān)信息并提交后,會(huì)彈出如下信息,表示注冊(cè)成功:
register-mysql
再看下mysql 里的信息:
mysql> select * from tblmember;
+----+-------+-------+------------------+------------------------------------------+-------------+--------+
| id | fName | lName | email | password | birthdate | gender |
+----+-------+-------+------------------+------------------------------------------+-------------+--------+
| 10 | test | yang | admin@361way.com | a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 | 1997-Jan-28 | Female |
| 11 | aaa | bbb | test@91it.org | 54df472f438b86fc96d68b8454183394ef26b8ac | 1997-Jan-18 | Female |
+----+-------+-------+------------------+------------------------------------------+-------------+--------+
2 rows in set (0.00 sec)
我執(zhí)行了兩次注冊(cè),這里有兩臺(tái)記錄。