一個簡單的註冊頁面(php+mysql)
阿新 • • 發佈:2019-02-19
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!--Form-->
<form action="#" method="post">
<h1>Register</h1>
<p>First Name:<input type="text" name="fn"></p>
<p>Last Name:<input type="text" name="ln"></p>
<p>Eamil Address:<input type="text" name="email"></p>
<p>Password:<input type="text" name="ps1"></p>
<p>Confirm Password:<input type="text" name="ps2"></p>
<input type="submit" value="Register">
</form>
<?php
// require the connection file
require('connect.php');
//if posted
if($_SERVER['REQUEST_METHOD']=='POST'){
$errors=array();
//check first name
if(empty($_POST['fn'])){
$errors[]='Please input your first name';
}
else{
$fn=$_POST['fn'];
}
//check last name
if(empty($_POST['ln'])){
$errors[]='Please input your last name';
}
else{
$ln=$_POST['ln'];
}
//check email address
if(empty($_POST['email'])){
$errors[]='Please input your email address';
}
else{
$email=$_POST['email'];
}
//check password
if(empty($_POST['ps1'])){
$errors[]='Please input your password';
}
else{
$ps1=$_POST['ps1'];
}
//confirm password
if(empty($_POST['ps2'])){
$errors[]='Please input your password again';
}
else{
if($_POST['ps1']!=$_POST['ps2']){
$errors[]='Please confirm your password';
}
else{
$p=$_POST['ps1'];
}
}
//Print errors when they exist
if(!empty($errors)){
echo"Error(s):";
foreach($errors as $msg){
echo "<p>$msg</p>";
}
}
else{
//record the registration information
$q="Insert into users(first_name,last_name,email,pass,registration_date)"
. "values('$fn','$ln','$email',MD5('$ps1'),now())";
$r=mysqli_query($dbc,$q);
if($r){
echo"Registraion successed!";
}
else{
echo"Failed to register. Please check your information";
}
}
}
?>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!--Form-->
<form action="#" method="post">
<h1>Register</h1>
<p>First Name:<input type="text" name="fn"></p>
<p>Last Name:<input type="text" name="ln"></p>
<p>Eamil Address:<input type="text" name="email"></p>
<p>Password:<input type="text" name="ps1"></p>
<p>Confirm Password:<input type="text" name="ps2"></p>
<input type="submit" value="Register">
</form>
<?php
// require the connection file
require('connect.php');
//if posted
if($_SERVER['REQUEST_METHOD']=='POST'){
$errors=array();
//check first name
if(empty($_POST['fn'])){
$errors[]='Please input your first name';
}
else{
$fn=$_POST['fn'];
}
//check last name
if(empty($_POST['ln'])){
$errors[]='Please input your last name';
}
else{
$ln=$_POST['ln'];
}
//check email address
if(empty($_POST['email'])){
$errors[]='Please input your email address';
}
else{
$email=$_POST['email'];
}
//check password
if(empty($_POST['ps1'])){
$errors[]='Please input your password';
}
else{
$ps1=$_POST['ps1'];
}
//confirm password
if(empty($_POST['ps2'])){
$errors[]='Please input your password again';
}
else{
if($_POST['ps1']!=$_POST['ps2']){
$errors[]='Please confirm your password';
}
else{
$p=$_POST['ps1'];
}
}
//Print errors when they exist
if(!empty($errors)){
echo"Error(s):";
foreach($errors as $msg){
echo "<p>$msg</p>";
}
}
else{
//record the registration information
$q="Insert into users(first_name,last_name,email,pass,registration_date)"
. "values('$fn','$ln','$email',MD5('$ps1'),now())";
$r=mysqli_query($dbc,$q);
if($r){
echo"Registraion successed!";
}
else{
echo"Failed to register. Please check your information";
}
}
}
?>
</body>
</html>