By GokiSoft.com| 19:48 26/05/2022|
Học PHP

[Video] Bài tập - Tạo website bán hàng lư niệm - Login - Lập trình PHP/MySQL - C2110L

Bài tập - Tạo website bán hàng lư niệm - Login - Lập trình PHP/MySQL



#readme.txt


Hướng dẫn phát triển dự án:
1) Thiết kế CSDL
create database if not exists BT2284

create table if not exists users (
	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(250),
	thumbnail varchar(500),
	content text,
	price float,
	created_at datetime,
	updated_at datetime,
	id_user int references users (id)
)

2) Xậy dựng khung dự án
	- config.php -> Chứa cấu hình thông tin kết nối CSDL
	- dbhelp.php -> Chưa các hàm sử dụng trong dự án -> kết nối CSDL
	- init.php -> Khởi tạo database
	- user
		- login.php
		- register.php
	- gift
		- index.php -> Hiển thị danh sách sản phẩm
		- add.php
		- edit.php
		- delete.php

3) Phát triển dự án


#config.php


<?php
define('HOST', 'localhost');
define('DATABASE', 'BT2284');
define('USERNAME', 'root');
define('PASSWORD', '');

const SQL_CREATE_DB = 'create database if not exists '.DATABASE;
const SQL_CREATE_USERS = 'create table if not exists users (
			id int primary key auto_increment,
			fullname varchar(50),
			email varchar(150),
			password varchar(32),
			token varchar(32)
		)';
const SQL_CREATE_GIFT = 'create table if not exists gift (
			id int primary key auto_increment,
			title varchar(250),
			thumbnail varchar(500),
			content text,
			price float,
			created_at datetime,
			updated_at datetime,
			id_user int references users (id)
		)';


#dbhelp.php


<?php
require_once('config.php');

function init() {
	// Them du lieu vao CSDL
	// B1. Cach ket noi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD);
	mysqli_set_charset($conn, 'utf8');

	// B2. Luu du lieu
	// $sql = "delete from student where id = $id";
	mysqli_query($conn, SQL_CREATE_DB);

	// B3. Ngat ket noi toi CSDL
	mysqli_close($conn);
}

/**
 * Insert, Update, Delete
 */
function execute($sql) {
	// Them du lieu vao CSDL
	// B1. Cach ket noi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	// B2. Luu du lieu
	// $sql = "delete from student where id = $id";
	mysqli_query($conn, $sql);

	// B3. Ngat ket noi toi CSDL
	mysqli_close($conn);
}

/**
 * Select
 */
function executeResult($sql, $isSingle = false) {
	// Them du lieu vao CSDL
	// B1. Cach ket noi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	// B2. Luu du lieu
	$resultset = mysqli_query($conn, $sql);

	if($isSingle) {
		$data = mysqli_fetch_array($resultset, 1);
	} else {
		$data = [];
		while(($row = mysqli_fetch_array($resultset, 1)) != null) {
			$data[] = $row;
		}
	}

	// B3. Ngat ket noi toi CSDL
	mysqli_close($conn);

	return $data;
}


#init.php


<?php
// var_dump($_POST);
if(!empty($_POST)) {
	//Thuc hien khoi tao CSDL
	require_once('dbhelp.php');

	init();
	execute(SQL_CREATE_USERS);
	execute(SQL_CREATE_GIFT);
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Init Database</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container text-center">
	<form method="post">
		<button name="action" value="init" class="btn btn-lg btn-warning" style="width: 260px; margin-top: 50px">Init Database</button>
	</form>
</div>
</body>
</html>


#gift/add.php


<?php
session_start();

if(!isset($_SESSION['user'])) {
	header('Location: ../user/login.php');
	die();
}

//Thuc hien khoi tao CSDL
require_once('../dbhelp.php');

if(!empty($_POST)) {
	$title = $_POST['title'];
	$thumbnail = $_POST['thumbnail'];
	$price = $_POST['price'];
	$content = $_POST['content'];

	$createdAt = $updatedAt = date('Y-m-d H:i:s');

	$sql = "insert into gift(title, thumbnail, price, content, updated_at, created_at) values ('$title', '$thumbnail', '$price', '$content', '$updatedAt', '$createdAt')";
	execute($sql);
	header('Location: index.php');
	die();
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Add Gift Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="post">
		<div class="form-group">
			<label>Title: </label>
			<input type="text" name="title" class="form-control">
		</div>
		<div class="form-group">
			<label>Thumbnail: </label>
			<input type="text" name="thumbnail" class="form-control">
		</div>
		<div class="form-group">
			<label>Price: </label>
			<input type="number" name="price" class="form-control">
		</div>
		<div class="form-group">
			<label>Content: </label>
			<textarea class="form-control" rows="10" name="content"></textarea>
		</div>
		<div class="form-group">
			<p>
				<a href="index.php">Back list page</a>
			</p>
			<button class="btn btn-success">Add</button>
		</div>
	</form>
</div>
</body>
</html>


#gift/delete.php


<?php
session_start();

if(!isset($_SESSION['user'])) {
	header('Location: ../user/login.php');
	die();
}

//Thuc hien khoi tao CSDL
require_once('../dbhelp.php');

if(!empty($_POST)) {
	$id = $_POST['id'];
	$sql = "delete from gift where id = $id";
	execute($sql);

	header('Location: index.php');
	die();
}

$id = $_GET['id'];
$sql = "select * from gift where id = $id";
$item = executeResult($sql, true);

$title  = $item['title'];
$thumbnail  = $item['thumbnail'];
$price  = $item['price'];
$content  = $item['content'];
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Delete Gift Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="post">
		<div class="form-group">
			<label>Title: <?=$title?></label>
			<input type="text" name="id" value="<?=$id?>" style="display: none;">
		</div>
		<div class="form-group">
			<img src="<?=$thumbnail?>" style="max-width: 300px;">
		</div>
		<div class="form-group">
			<label>Price: <?=$price?></label>
		</div>
		<div class="form-group">
			<p>
				<a href="index.php">Back list page</a>
			</p>
			<button class="btn btn-danger">Continue to delete this gift</button>
		</div>
	</form>
</div>
</body>
</html>


#gift/edit.php


<?php
session_start();

if(!isset($_SESSION['user'])) {
	header('Location: ../user/login.php');
	die();
}

//Thuc hien khoi tao CSDL
require_once('../dbhelp.php');

if(!empty($_POST)) {
	$id = $_POST['id'];
	$title = $_POST['title'];
	$thumbnail = $_POST['thumbnail'];
	$price = $_POST['price'];
	$content = $_POST['content'];

	$updatedAt = date('Y-m-d H:i:s');

	$sql = "update gift set title = '$title', thumbnail = '$thumbnail', price = '$price', content = '$content' , updated_at = '$updatedAt' where id = $id";
	execute($sql);
	header('Location: index.php');
	die();
}

$id = $_GET['id'];
$sql = "select * from gift where id = $id";
$item = executeResult($sql, true);

$title  = $item['title'];
$thumbnail  = $item['thumbnail'];
$price  = $item['price'];
$content  = $item['content'];
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Add Gift Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="post">
		<div class="form-group">
			<label>Title: </label>
			<input type="text" name="title" class="form-control" value="<?=$title?>">
			<input type="text" name="id" value="<?=$id?>" style="display: none;">
		</div>
		<div class="form-group">
			<label>Thumbnail: </label>
			<input type="text" name="thumbnail" class="form-control" value="<?=$thumbnail?>">
		</div>
		<div class="form-group">
			<label>Price: </label>
			<input type="number" name="price" class="form-control" value="<?=$price?>">
		</div>
		<div class="form-group">
			<label>Content: </label>
			<textarea class="form-control" rows="10" name="content"><?=$content?></textarea>
		</div>
		<div class="form-group">
			<p>
				<a href="index.php">Back list page</a>
			</p>
			<button class="btn btn-warning">Update</button>
		</div>
	</form>
</div>
</body>
</html>


#gift/index.php


<?php
session_start();

if(!isset($_SESSION['user'])) {
	header('Location: ../user/login.php');
	die();
}

//Thuc hien khoi tao CSDL
require_once('../dbhelp.php');

$sql = "select * from gift";
$giftList = executeResult($sql);
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Gift Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<a href="add.php"><button class='btn btn-success mb-3'>Add New Gift</button></a>
	<table class="table table-bordered">
		<thead>
			<tr>
				<th>No</th>
				<th>Thumbnail</th>
				<th>Title</th>
				<th>Price</th>
				<th style="width: 60px"></th>
				<th style="width: 60px"></th>
			</tr>
		</thead>
		<tbody>
<?php
$index = 0;
foreach ($giftList as $item) {
	echo "<tr>
			<td>".++$index."</td>
			<td><img src='".$item['thumbnail']."' style='width: 220px'/></td>
			<td>".$item['title']."</td>
			<td>".number_format($item['price'], 0)."</td>
			<td><a href='edit.php?id=".$item['id']."'><button class='btn btn-warning'>Edit</button></a></td>
			<td><a href='delete.php?id=".$item['id']."'><button class='btn btn-danger'>Delete</button></a></td>
		</tr>";
}
?>
		</tbody>
	</table>
</div>
</body>
</html>


#user/login.php


<?php
session_start();

if(isset($_SESSION['user'])) {
	header('Location: ../gift/index.php');
	die();
}

//Thuc hien khoi tao CSDL
require_once('../dbhelp.php');

$msg = $email = $pwd = "";
if(!empty($_POST)) {
	$email = $_POST['email'];
	$pwd = $_POST['pwd'];

	$sql = "select * from users where email = '$email' and password = '$pwd'";
	$user = executeResult($sql, true);

	if($user != null) {
		//login thanh cong
		$_SESSION['user'] = $user;
		header('Location: ../gift/index.php');
	} else {
		$msg = "Email | Password is not correct";
	}
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Login Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="post">
		<h2 class="text-center" style="color: red"><?=$msg?></h2>
		<div class="form-group">
			<label>Email: </label>
			<input type="email" name="email" class="form-control">
		</div>
		<div class="form-group">
			<label>Password: </label>
			<input type="password" name="pwd" class="form-control">
		</div>
		<div class="form-group">
			<p>
				<a href="register.php">Create a new account</a>
			</p>
			<button class="btn btn-success">Login</button>
		</div>
	</form>
</div>
</body>
</html>


#user/register.php


<?php
session_start();

if(isset($_SESSION['user'])) {
	header('Location: ../gift/index.php');
	die();
}

//Thuc hien khoi tao CSDL
require_once('../dbhelp.php');

$msg = $fullname = $email = $pwd = $confirmPwd = "";
if(!empty($_POST)) {
	$fullname = $_POST['fullname'];
	$email = $_POST['email'];
	$pwd = $_POST['pwd'];
	$confirmPwd = $_POST['confirmPwd'];

	if($pwd != $confirmPwd) {
		$msg = "Password not match";
	} else {
		$sql = "insert into users (fullname, email, password) values ('$fullname', '$email', '$pwd')";
		execute($sql);
	}
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Register Page</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<form method="post">
		<h2 class="text-center" style="color: red"><?=$msg?></h2>
		<div class="form-group">
			<label>Full Name: </label>
			<input type="text" name="fullname" class="form-control">
		</div>
		<div class="form-group">
			<label>Email: </label>
			<input type="email" name="email" class="form-control">
		</div>
		<div class="form-group">
			<label>Password: </label>
			<input type="password" name="pwd" class="form-control">
		</div>
		<div class="form-group">
			<label>Confirm Password: </label>
			<input type="password" name="confirmPwd" class="form-control">
		</div>
		<div class="form-group">
			<p>
				<a href="login.php">I have a account</a>
			</p>
			<button class="btn btn-success">Register</button>
		</div>
	</form>
</div>
</body>
</html>


Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó