Bài tập - Tạo website bán hàng lư niệm - Login - Lập trình PHP/MySQL
Bạn được yêu cầu phát triển dự án như sau
- Phát triển 1 trang init.php thực hiện các chức năng sau.
1) Tạo CSDL đặt tên là gift_db -> nếu chưa tồn tại
2) Tạo bảng gift nếu chưa tồn tại gồm các column sau: id tự tăng, tiêu đề, thumbnail, nội dung, giá tiền, ngày tạo, ngày sửa, id_user
Bảng user: id tự tăng, tên, email, mật khẩu, token.
- Trang login.php -> sau khi login thành công thì chuyển sang trang quantri.php
- Trang register.php -> sau khi đăng ký thành công thì chuyển sang trang login.php
- quantri.php -> Hiển thị thông tin gift trong database. Cho phép thêm/sửa/xoá -> thực hiện viết phân trang. Chỉ xem khi tài khoản đã login -> và chỉ xem được sản phẩm mình đã thêm vào.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![GokiSoft.com [Teacher]](https://www.gravatar.com/avatar/fc6ba9324e017d540af3613b3a77dd21.jpg?s=80&d=mm&r=g)
GokiSoft.com
2021-07-17 12:49:42
1) Xay dung database
- create database if not exists gift_db_2
- create tables
create table if not exists user (
id int primary key auto_increment,
fullname varchar(50),
email varchar(150),
password varchar(32),
token varchar(32)
)
create table if not exists gift (
id int primary key auto_increment,
title varchar(200) not null,
thumbnail varchar(500),
content longtext,
price float,
created_at datetime,
updated_at datetime,
id_user int references user (id)
)
2) Phat trien phan mem
- Xay dung khung chuong trinh
db
config.php
dbhelper.php
![GokiSoft.com [Teacher]](https://www.gravatar.com/avatar/fc6ba9324e017d540af3613b3a77dd21.jpg?s=80&d=mm&r=g)
GokiSoft.com
2021-07-17 12:48:33
#config.php
<?php
define('HOST', 'localhost');
define('DATABASE', 'gift_db_2');
define('USERNAME', 'root');
define('PASSWORD', '');
#register.php
<?php
session_start();
require_once ('db/dbhelper.php');
require_once ('form_register.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Register Page</title>
<style type="text/css">
.main {
width: 640px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="main">
<h1 style="text-align: center;">Register Form</h1>
<form method="post">
<label>Full Name: </label>
<input required="true" type="text" name="fullname" style="width: 100%;" placeholder="Enter full name">
<br/><br/>
<label>Email: </label>
<input required="true" type="email" name="email" style="width: 100%;" placeholder="Enter email">
<br/><br/>
<label>Password: </label>
<input required="true" type="password" name="password" style="width: 100%;" placeholder="Enter password">
<br/><br/>
<label>Confirm Password: </label>
<input required="true" type="password" name="confirm_password" style="width: 100%;" placeholder="Enter confirm password">
<br/><br/>
<p>
<a href="login.php">I have a account</a>
</p>
<button type="submit">Register</button>
</form>
</div>
</body>
</html>
#quantri.php
<?php
session_start();
require_once ('db/dbhelper.php');
$sql = "select gift.*, user.fullname from gift left join user on gift.id_user = user.id";
$result = executeResult($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>Gift Page</title>
<style type="text/css">
.main {
width: 640px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="main">
<h1 style="text-align: center;">Gift Page</h1>
<a href="gift_editor.php"><button>Add new gift</button></a>
<table border="1" cellspacing="3" cellpadding="3" style="width: 100%;margin-top: 10px;">
<thead>
<tr>
<th>No</th>
<th>Title</th>
<th>Price</th>
<th>Updated At</th>
<th>Created By</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$count = 0;
foreach ($result as $item) {
echo '<tr>
<td>'.(++$count).'</td>
<td>'.$item['title'].'</td>
<td>'.$item['price'].'</td>
<td>'.$item['updated_at'].'</td>
<td>'.$item['fullname'].'</td>
<td><a href="gift_editor.php?id='.$item['id'].'"><button>Edit</button></a></td>
<td>
<form method="post" action="form_delete_gift.php" onsubmit="return confirmDelete();">
<input type="text" name="id" style="width: 100%;" placeholder="Enter id" value="'.$item['id'].'" hidden>
<button>Delete</button>
</form>
</td>
</tr>';
}
?>
</tbody>
</table>
</div>
<script type="text/javascript">
function confirmDelete() {
option = confirm('Ban chac chan muon xoa gift nay ko?')
if(!option) return false
return true
}
</script>
</body>
</html>
#login.php
<?php
session_start();
require_once ('db/dbhelper.php');
require_once ('form_login.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<style type="text/css">
.main {
width: 640px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="main">
<h1 style="text-align: center;">Login Form</h1>
<form method="post">
<label>Email: </label>
<input required="true" type="email" name="email" style="width: 100%;" placeholder="Enter email">
<br/><br/>
<label>Password: </label>
<input required="true" type="password" name="password" style="width: 100%;" placeholder="Enter password">
<br/><br/>
<p>
<a href="login.php">Create a new account</a>
</p>
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
#init.php
<?php
if (!empty($_POST)) {
require_once ('db/dbhelper.php');
createDB();
$sql = "create table if not exists user (
id int primary key auto_increment,
fullname varchar(50),
email varchar(150),
password varchar(32),
token varchar(32)
)";
execute($sql);
$sql = "create table if not exists gift (
id int primary key auto_increment,
title varchar(200) not null,
thumbnail varchar(500),
content longtext,
price float,
created_at datetime,
updated_at datetime,
id_user int references user (id)
)";
execute($sql);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Init Page</title>
</head>
<body>
<h1 style="text-align: center;">Init Database</h1>
<center>
<form method="post">
<button name="action" value="init">Start Init Database</button>
</form>
</center>
</body>
</html>
#gift_editor.php
<?php
session_start();
require_once ('db/dbhelper.php');
require_once ('form_gift.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Gift Editor Page</title>
<style type="text/css">
.main {
width: 640px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="main">
<h1 style="text-align: center;">Gift Editor</h1>
<form method="post">
<label>Title: </label>
<input type="text" name="id" style="width: 100%;" placeholder="Enter id" value="<?=$id?>" hidden>
<input required="true" type="text" name="title" style="width: 100%;" placeholder="Enter title" value="<?=$title?>">
<br/><br/>
<label>Thumbnail: </label>
<input required="true" type="text" name="thumbnail" style="width: 100%;" placeholder="Enter thumbnail" value="<?=$thumbnail?>">
<br/><br/>
<label>Price: </label>
<input required="true" type="number" name="price" style="width: 100%;" placeholder="Enter price" value="<?=$price?>">
<br/><br/>
<label>Content: </label>
<textarea name="content" rows="5" style="width: 100%;"><?=$content?></textarea>
<button type="submit">Save</button>
</form>
</div>
</body>
</html>
#form_register.php
<?php
$fullname = $email = $password = $confirm_password = "";
if (!empty($_POST)) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
if ($confirm_password == $password) {
$sql = "insert into user (fullname, email, password) values ('$fullname', '$email', '$password')";
execute($sql);
header('Location: login.php');
die();
}
}
#form_login.php
<?php
$fullname = $email = $password = $confirm_password = "";
if (!empty($_POST)) {
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "select * from user where email = '$email' and password = '$password'";
$result = executeResult($sql);
if (count($result) > 0) {
//login thanh cong
$_SESSION['user'] = $result[0];
header("Location: quantri.php");
die();
}
}
#form_gift.php
<?php
$title = $thumbnail = $content = $price = "";
if (!empty($_POST)) {
$id = $_POST['id'];
$title = $_POST['title'];
$thumbnail = $_POST['thumbnail'];
$content = $_POST['content'];
$price = $_POST['price'];
$id_user = $_SESSION['user']['id'];
$created_at = $updated_at = date('Y-m-d H:i:s');
if ($confirm_password == $password) {
if ($id > 0) {
$sql = "update gift set title = '$title', thumbnail = '$thumbnail', content = '$content', price = '$price', updated_at = '$updated_at' where id = $id";
} else {
$sql = "insert into gift (title, thumbnail, content, price, id_user, created_at, updated_at) values ('$title', '$thumbnail', '$content', '$price', '$id_user', '$created_at', '$updated_at')";
}
execute($sql);
header('Location: quantri.php');
die();
}
}
$id = '0';
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "select * from gift where id = ".$id;
$result = executeResult($sql);
if (count($result) > 0) {
$title = $result[0]['title'];
$thumbnail = $result[0]['thumbnail'];
$content = $result[0]['content'];
$price = $result[0]['price'];
} else {
$id = '0';
}
}
#form_delete_gift.php
<?php
session_start();
require_once ('db/dbhelper.php');
if (!empty($_POST)) {
$id = $_POST['id'];
$sql = "delete from gift where id = $id";
execute($sql);
header('Location: quantri.php');
die();
}
#dbhelper.php
<?php
require_once ('config.php');
/**
* Su dung cho lenh: insert/update/delete
*/
function createDB() {
// Them du lieu vao database
//B1. Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD);
//connect error
if (mysqli_connect_errno()) {
echo "database error > ".mysqli_connect_error();
exit();
}
mysqli_set_charset($conn, 'utf8');
//B2. Thuc hien truy van insert
$sql = "create database if not exists ".DATABASE;
mysqli_query($conn, $sql);
//B3. Dong ket noi database
mysqli_close($conn);
}
/**
* Su dung cho lenh: insert/update/delete
*/
function execute($sql) {
// Them du lieu vao database
//B1. Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
//connect error
if (mysqli_connect_errno()) {
echo "database error > ".mysqli_connect_error();
exit();
}
mysqli_set_charset($conn, 'utf8');
//B2. Thuc hien truy van insert
mysqli_query($conn, $sql);
//B3. Dong ket noi database
mysqli_close($conn);
}
/**
* Su dung cho lenh: select
*/
function executeResult($sql) {
// Them du lieu vao database
//B1. Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
//connect error
if (mysqli_connect_errno()) {
echo "database error > ".mysqli_connect_error();
exit();
}
mysqli_set_charset($conn, 'utf8');
//B2. Thuc hien truy van insert
$resultset = mysqli_query($conn, $sql);
$data = [];
while (($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row;
}
//B3. Dong ket noi database
mysqli_close($conn);
return $data;
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2021-05-10 06:59:29
#config.php
<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'T2008A');
#database-helper.php
<?php
require_once ('config.php');
//insert, update, delete
function execute($sql){
//Mo lien ket
$conn = mysqli_connect(HOST,USERNAME,PASSWORD,DATABASE);
//He utf8
mysqli_set_charset($conn,'utf8');
//Thuc thi cau lenh
mysqli_query($conn,$sql);
//Dong cong
mysqli_close($conn);
}
//Doi voi bai toan muon lay du lieu
function executeResult($sql) {
$conn = mysqli_connect(HOST,USERNAME,PASSWORD,DATABASE);
mysqli_set_charset($conn,'utf8');
$data = [];
$result = mysqli_query($conn,$sql);
while(($row = mysqli_fetch_array($result,1)) != null){
$data[] = $row;
}
mysqli_close($conn);
return $data;
}
function removeSpecialCharacter($str) {
$str = str_replace('\\', '\\\\', $str);
$str = str_replace('\'', '\\\'', $str);
return $str;
}
function getPOST($key) {
$value = '';
if (isset($_POST[$key])) {
$value = $_POST[$key];
}
return removeSpecialCharacter($value);
}
#login.php
<?php
require_once('login-form.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<style>
body{
background: linear-gradient(to right, #D9AFD9, #97D9E1);
}
a{
text-decoration: none !important;
}
.link{
margin-top: 20px;
}
</style>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Login</h2>
</div>
<div class="panel-body">
<form method="post">
<div class="form-group">
<label for="username">Username:</label>
<input required="true" type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input required="true" type="password" class="form-control" id="password" name="password">
</div>
<button class="btn btn-success">Login</button>
<div class="link"><a href="register.php">Chưa có tài khoản? Đăng kí tại đây</a></div>
</form>
</div>
</div>
</div>
</body>
</html>
#login-form.php
<?php
function alert($msg) {
echo "<script type='text/javascript'>alert('$msg');</script>";
}
require_once('database-helper.php');
$accountList = executeResult('select * from account');
$username = $password = '';
if(!empty($_POST)){
$username = getPOST('username');
$password = getPOST('password');
foreach ($accountList as $account) {
if($account['username'] == $username){
if($account['password'] == $password){
header('Location: quantri.php?name='.$account['name']);
die();
}
}
}
alert('Khong co tai khoan');
}
#quantri.php
<?php
require_once('database-helper.php');
require_once('quantri-form.php');
$productList = executeResult('select * from gift_db');
?>
<!doctype html>
<html lang="en">
<head>
<title>Title</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<style>
body{
background: linear-gradient(to right, #D9AFD9, #97D9E1);
}
h2{
color: red;
text-align: center;
}
a{
text-decoration: none !important;
}
th,td{
background: #dad0d0 !important;
color: green;
}
.table-bordered td, .table-bordered th{
border-color: black !important;
}
#formAdd{
opacity: 0;
height: 0;
overflow: hidden;
}
.container{
margin-bottom: 50px;
}
.card{
margin-top: 20px;
}
.card-header{
background: paleturquoise !important;
text-align: center;
color: palevioletred;
font-weight: 600;
font-size: 25px;
}
.card form{
margin: 20px;
}
.close {
cursor: pointer;
position: absolute;
top: 4.9%;
left: 0%;
padding: 20.5px 20px;
transform: translate(0%, -50%);
}
.close:hover{
background: blueviolet;
}
</style>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<h2>Xin chào <?=$_GET['name']?></h2>
<div class="container">
<table class="table table-bordered">
<thead>
<tr>
<td>No</td>
<td>Name</td>
<td>Image</td>
<td>Content</td>
<td>Price</td>
<td>Created Time</td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<?php
$count = 0;
foreach ($productList as $item) {
echo '<tr>
<td>'.(++$count).'</td>
<td>'.$item['name'].'</td>
<td><img src="'.$item['thumbnail'].'" style="width: 160px"/></td>
<td>'.$item['content'].'</td>
<td>'.$item['price'].'</td>
<td>'.$item['updated_at'].'</td>
<td><button class="btn btn-warning"
onclick="editProduct('.$item['id'].',\''.$item['name'].'\',\''.$item['thumbnail'].'\',\''.$item['content'].'\','.$item['price'].')">Edit</button></td>
<td><button onclick="deleteProduct('.$item['id'].')" class="btn btn-danger">Delete</button></td>
</tr>';
}
?>
</tbody>
</table>
<div type="button" class="btn btn-success" onclick="addProduct()">Add product</div>
<a href="login.php" style="margin-left: 10px;">Đăng xuất</a>
<div id="formAdd" class="card">
<div class="card-header"><span class="close" onclick="CloseForm()">×</span> <span id="formName"> Add product </span></div>
<div class="card-body">
<form action="" method="post">
<div class="form-group">
<label for="name">Name:</label>
<input required type="text" id="name" name="name" class="form-control">
</div>
<div class="form-group">
<label for="thumbnail">Image:</label>
<input required type="text" id="thumbnail" name="thumbnail" class="form-control">
</div>
<div class="form-group">
<label for="content">Content:</label>
<textarea required class="form-control" id="content" rows="5" id="content" name="content"></textarea>
</div>
<div class="form-group">
<label for="price">Price:</label>
<input required type="text" name="price" id="price" class="form-control">
</div>
<input type="text" id='abc' name="id" hidden >
<button name="" id="buttonName" class="btn btn-primary" btn-lg btn-block">Add Product</button>
</form>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<script>
function deleteProduct(id) {
option = confirm('Are you sure to delete this product?')
if(!option) return
$.post('quantri-form.php', {
'action': 'delete',
'id': id
}, function(data) {
location.reload()
})
}
var formAdd = document.getElementById('formAdd')
var formName = document.getElementById('formName')
var buttonName = document.getElementById('buttonName')
var idInput = document.getElementById('abc')
function addProduct(){
formName.textContent = 'Add Product'
formAdd.style.opacity = "1"
formAdd.style.height = "auto"
formAdd.style.transition = 'opacity 0.5s'
buttonName.textContent = 'Add Product'
idInput.value ='';
}
function editProduct(id,name,thumbnail,content,price){
formName.textContent = 'Edit Product'
formAdd.style.opacity = "1"
formAdd.style.height = "auto"
formAdd.style.transition = 'opacity 0.5s'
buttonName.textContent = 'Edit Product'
idInput.value = id;
document.getElementById('name').value = name;
document.getElementById('thumbnail').value = thumbnail;
document.getElementById('content').value = content;
document.getElementById('price').value = price;
}
function CloseForm(){
formAdd.style.opacity = "0"
formAdd.style.transition = 'opacity 0s'
formAdd.style.height = "0"
}
</script>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<!-- <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> -->
</body>
</html>
#quantri-form.php
<?php
require_once('database-helper.php');
$name = $thumbnail = $content = $price = $idEdit ='' ;
if(!empty($_POST)){
$id = getPOST('id');
$action = getPOST('action');
switch($action){
case 'delete':
deleteProduct($id);
break;
default:
if(empty($id)){
AddProduct();
}else{
EditProduct($id);
}
break;
}
}
function deleteProduct($id) {
$sql = "delete from gift_db where id = $id";
execute($sql);
}
function addProduct() {
$name = getPOST('name');
$thumbnail = getPOST('thumbnail');
$content = getPOST('content');
$price = getPOST('price');
$created_at = $updated_at = date('Y-m-d H:i:s');
if(!empty($name)){
$sql = "insert into gift_db(name,thumbnail,content,price,created_at,updated_at)
values ('$name','$thumbnail','$content','$price','$created_at','$updated_at')";
execute($sql);
}
}
function EditProduct($id){
$name = getPOST('name');
$thumbnail = getPOST('thumbnail');
$content = getPOST('content');
$price = getPOST('price');
$updated_at = date('Y-m-d H:i:s');
if(!empty($name)){
$sql = "update gift_db set name ='$name',thumbnail = '$thumbnail',content = '$content',price = '$price',updated_at = '$updated_at'
where id = $id";
execute($sql);
}
}
#register.php
<?php
require_once('register-form.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<style>
body{
background: linear-gradient(to right, #D9AFD9, #97D9E1);
}
a{
text-decoration: none !important;
}
.link{
margin-top: 20px;
}
</style>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Register</h2>
</div>
<div class="panel-body">
<form method="post">
<div class="form-group">
<label for="name">Name:</label>
<input required="true" type="text" class="form-control" id="name" name="name">
</div>
<div class="form-group">
<label for="username">Username:</label>
<input required="true" type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input required="true" type="password" class="form-control" id="password" name="password">
</div>
<div class="form-group">
<label for="passwordConfirmed">Confirm Password</label>
<input required="true" type="password" class="form-control" id="passwordConfirmed" name="passwordConfirmed">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input required="true" type="email" class="form-control" id="email" name="email">
</div>
<button class="btn btn-success">Register</button>
<div class="link"><a href="login.php">Login</a></div>
</form>
</div>
</div>
</div>
</body>
</html>
#register-form.php
<?php
require_once('database-helper.php');
$accountList = executeResult('select * from account');
function alert($msg) {
echo "<script type='text/javascript'>alert('$msg');</script>";
}
$username = $name = $email = $password = $passwordConfirmed = '';
if (!empty($_POST)) {
$username = getPOST('username');
$name = getPOST('name');
$email = getPOST('email');
$password = getPOST('password');
$passwordConfirmed = getPOST('passwordConfirmed');
$check = 0;
foreach ($accountList as $account) {
if($account['username'] == $username){
$check++;
alert('Tai khoan da ton tai');
break;
}
}
if($password == $passwordConfirmed && $check == 0){
$created_at = $updated_at = date('Y-m-d H:i:s');
$sql = "insert into account(name,username,password,email,created_at,updated_at)
values ('$name','$username','$password','$email','$created_at','$updated_at')";
execute($sql);
header('Location: login.php');
}
}