<?php
//Start session session
session_start();
header('content-type:text/html;charset=utf-8');
//Receive form data
$username=trim($_POST['username']);
$password=md5(trim($_POST['password'])); //Password MD5 encryption
$password_confirm=md5(trim($_POST['password']));
$code=$_POST['code'];
if($username==''||$password==''||$password_confirm==''){
echo "<script>alert('Please make sure the information is complete!');history.back();</script>";
}elseif($code!=$_SESSION['var_code']){
echo "<script>alert('The verification code is incorrect!');history.back();</script>";
}else{
if($password==$password_confirm){
//2. Connect to MYSQL and select the database
$link = mysql_connect('localhost','root','root') or die("Database connection failed!");
mysql_query('set names utf8');
mysql_query('use `demodb`');
//Execute sql
$sql="select * from `manager` where `username`='$username'";
$res=mysql_query($link,$sql);
$nums=mysql_num_rows($res);
if($nums){ //Exists
echo "<script> alert('Username already exists, please change it!');history.back();</script>";
exit;
}else{
$sql_insert="insert into manager values('$username','$password')";
$res_insert=mysql_query($link,$sql_insert);
if($res_insert){
echo "<script>alert('Registration successful!');history.back();</script>";
}else{
echo "<script>alert('Registration failed!');history.back();</script>";
}
}
}
else{
echo "<script>alert('Passwords are inconsistent!');history.back();</script>";
}
}
?>