php用户注册入库
上一篇 bootstrap+php实现注册页面 中,通过bootstrap + html + php写了一个简单的用户注册页面。本篇结合前一篇的内容,将注册页面上提交的信息post 给后面的页面register.php ,register.php将post的信息提交入库。
一、创建数据库与表结构
1、建库
1mysql> create database 361way character set utf8;
2Query OK, 1 row affected (0.00 sec)
上面我建了一个同我站点同命的库361way 。
2、创建表结构
1CREATE TABLE IF NOT EXISTS `tblmember` (
2 `id` int(11) NOT NULL AUTO_INCREMENT,
3 `fName` varchar(30) NOT NULL,
4 `lName` varchar(30) NOT NULL,
5 `email` varchar(50) NOT NULL,
6 `password` varchar(60) NOT NULL,
7 `birthdate` text NOT NULL,
8 `gender` varchar(20) NOT NULL,
9 PRIMARY KEY (`id`)
10) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
这里的字符编码我选择的是utf8 ,并且在该表的id值是从11开始的(前面预留了10个),数据库引擎类型用的InnoDB,具体可以根据自己的需求修改。
二、post提交php页面
向后端提交post请求的register.php代码如下:
1<?php
2//set up mysql connection
3mysql_connect("localhost", "root", "123456") or die(mysql_error());
4//select database
5mysql_select_db("361way") or die(mysql_error());
6//get the value from the posted data and store it to a respected variable
7$fName = $_POST['fName'];
8$lName = $_POST['lName'];
9$email = $_POST['email'];
10$reemail = $_POST['reemail'];
11$password = sha1($_POST['password']);
12$month = $_POST['month'];
13$day = $_POST['day'];
14$year = $_POST['year'];
15$gender = $_POST['optionsRadios'];
16$birthdate = $year . '-' . $month . '-' . $day;
17//insert data using insert into statement
18$query = "INSERT INTO tblmember(id, fName, lName, email, password, birthdate, gender)
19 VALUES (NULL, '{$fName}', '{$lName}', '{$email}', '{$password}', '{$birthdate}', '{$gender}')";
20//execute the query
21if (mysql_query($query)) {
22 //dislay a message box that the saving is successfully save
23 echo "<script type=\"text/javascript\">
24 alert(\"New member added successfully.\");
25 window.location = \"registration.php\"
26 </script>";
27} else
28 die("Failed: " . mysql_error());
29?>
上面的代码使用时,数据库的用户名密码及库名根据实际情况修改。
三、测试代码
按上一篇的注册页面输入相关信息并提交后,会弹出如下信息,表示注册成功:
再看下mysql 里的信息:
1mysql> select * from tblmember;
2+----+-------+-------+------------------+------------------------------------------+-------------+--------+
3| id | fName | lName | email | password | birthdate | gender |
4+----+-------+-------+------------------+------------------------------------------+-------------+--------+
5| 10 | test | yang | admin@361way.com | a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 | 1997-Jan-28 | Female |
6| 11 | aaa | bbb | test@91it.org | 54df472f438b86fc96d68b8454183394ef26b8ac | 1997-Jan-18 | Female |
7+----+-------+-------+------------------+------------------------------------------+-------------+--------+
82 rows in set (0.00 sec)
我执行了两次注册,这里有两台记录。
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/register-into-mysql/4469.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.