簡單的php使用者註冊入庫例子

來源:互聯網
上載者:User


 php寫了一個簡單的使用者註冊頁面。本篇結合前一篇的內容,將註冊頁面上提交的資訊post 給後面的頁面register.php ,register.php將post的資訊提交入庫。

一、建立資料庫與表結構

1、建庫

mysql> create database 361way character set utf8;
Query OK, 1 row affected (0.00 sec)
上面我建了一個同我網站同命的庫361way 。

2、建立表結構

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開始的(前面預留了10個),資料庫引擎類型用的InnoDB,具體可以根據自己的需求修改。

二、post提交php頁面

向後端提交post請求的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());
?>

上面的代碼使用時,資料庫的使用者名稱密碼及庫名根據實際情況修改。

三、測試代碼

按上一篇的註冊頁面輸入相關資訊並提交後,會彈出如下資訊,表示註冊成功:

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)


我執行了兩次註冊,這裡有兩台記錄。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.