By GokiSoft.com|
11:21 05/07/2021|
Học PHP
[Share Code] Bài tập - Tạo website bán hàng lư niệm - Login - Lập trình PHP/MySQL - C2010G - OverView
#register.php
<?php
session_start();
require_once ('db/dbhelper.php');
$msg = '';
if (!empty($_POST)) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirmPwd = $_POST['confirm_pwd'];
if (!empty($email) && $password == $confirmPwd) {
$password = md5($password);
$sql = "insert into user (fullname, email, password) values ('$fullname', '$email', '$password')";
execute($sql);
} else {
$msg = "Register failed";
}
}
?>
<html>
<head>
<title>Register page</title>
<meta charset="utf-8">
<style type="text/css">
.main-content {
width: 640px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="main-content">
<h2 style="text-align: center;">Register</h2>
<h2 style="text-align: center; color: red"><?=$msg?></h2>
<form method="post">
<label>Full Name: </label>
<input type="text" name="fullname" placeholder="Enter full name" required="true" style="width: 100%;">
<br/><br/>
<label>Email: </label>
<input type="email" name="email" placeholder="Enter email" required="true" style="width: 100%;">
<br/><br/>
<label>Password: </label>
<input type="password" name="password" placeholder="Enter password" required="true" style="width: 100%;">
<br/><br/>
<label>Confirm Pwd: </label>
<input type="password" name="confirm_pwd" placeholder="Enter confirm password" required="true" style="width: 100%;">
<br/><br/>
<a href="login.php">Login</a>
<br/><br/>
<button type="submit">Register</button>
</form>
</div>
</body>
</html>
#readme.txt
Tổng quan nội dung:
- MySQL:
- Khởi tạo database bằng code
- Khởi tạo tables bằng code
- Viết được các chức năng hiển thị/thêm/sửa/xoá
- Session/Cookie
======================================================
Mini Project: https://gokisoft.com/bai-tap-tao-website-ban-hang-lu-niem-login-lap-trinh-phpmysql.html
Bước phát triển dự án:
B1) Phan tich database
- create database if not exists gift_db
- Tables
create table if not exists user (
id int primary key auto_increment,
fullname varchar(50) not null,
email varchar(150),
password varchar(32)
)
create table if not exists gift (
id int primary key auto_increment,
title varchar(200),
thumbnail varchar(500),
content text,
price float,
created_at datetime,
updated_at datetime,
user_id int references user (id)
)
B2) Phat trien chuc nang cua du an
- Xay dung 1 khung chuong:
db
- config.php
- dbhelper.php
- Chuc nang
- login.php
- register.php
- gift.php -> Hien thi/them/sua/xoa
#login.php
<?php
session_start();
require_once ('db/dbhelper.php');
$msg = '';
if (!empty($_POST)) {
$email = $_POST['email'];
$password = $_POST['password'];
if (!empty($email) && !empty($password)) {
$password = md5($password);
$sql = "select * from user where email = '$email' and password = '$password'";
$data = executeResult($sql);
if (count($data) > 0) {
$_SESSION['user'] = $data[0];
header('Location: gift.php');
die();
} else {
$msg = "Login failed";
}
} else {
$msg = "Login failed";
}
}
?>
<html>
<head>
<title>Register page</title>
<meta charset="utf-8">
<style type="text/css">
.main-content {
width: 640px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="main-content">
<h2 style="text-align: center;">Login</h2>
<h2 style="text-align: center; color: red"><?=$msg?></h2>
<form method="post">
<label>Email: </label>
<input type="email" name="email" placeholder="Enter email" required="true" style="width: 100%;">
<br/><br/>
<label>Password: </label>
<input type="password" name="password" placeholder="Enter password" required="true" style="width: 100%;">
<br/><br/>
<a href="register.php">Create a new account</a>
<br/><br/>
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
#init.php
<?php
require_once ('db/dbhelper.php');
if (!empty($_POST)) {
init();
//create tables
$sql = 'create table if not exists user (
id int primary key auto_increment,
fullname varchar(50) not null,
email varchar(150),
password varchar(32)
)';
execute($sql);
$sql = 'create table if not exists gift (
id int primary key auto_increment,
title varchar(200),
thumbnail varchar(500),
content text,
price float,
created_at datetime,
updated_at datetime,
user_id int references user (id)
)';
execute($sql);
}
?>
<html>
<head>
<title>Init page</title>
<meta charset="utf-8">
<style type="text/css">
.main-content {
width: 640px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="main-content">
<h2 style="text-align: center;">Register</h2>
<form method="post">
<button type="submit" name="action" value="init" style="width: 100%;">Init Database</button>
</form>
</div>
</body>
</html>
#gift.php
<?php
session_start();
require_once ('db/dbhelper.php');
$sql = "select * from gift";
$data = executeResult($sql);
?>
<html>
<head>
<title>Gift page</title>
<meta charset="utf-8">
<style type="text/css">
.main-content {
width: 640px;
margin: 0px auto;
}
</style>
<!-- jQuery library -->
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> -->
</head>
<body>
<div class="main-content">
<h2 style="text-align: center;">Gift</h2>
<a href="add-gift.php"><button>Add a new gift</button></a>
<table border="1" cellspacing="1" cellpadding="3" style="width: 100%; margin-top: 20px;">
<thead>
<tr>
<th>No</th>
<th>Thumbnail</th>
<th>Title</th>
<th>Price</th>
<th>Updated At</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$count = 0;
foreach ($data as $item) {
echo '<tr>
<td>'.(++$count).'</td>
<td><img src="'.$item['thumbnail'].'" style="width: 120px;"/></td>
<td>'.$item['title'].'</td>
<td>'.$item['price'].'</td>
<td>'.$item['updated_at'].'</td>
<td><a href="add-gift.php?id='.$item['id'].'"><button>Edit</button></a></td>
<td>
<form action="delete-gift.php" method="post" onsubmit="return deleteItem();">
<input type="text" name="id" value="'.$item['id'].'" hidden/>
<button>Remove</button>
</form>
</td>
</tr>';
}
?>
</tbody>
</table>
</div>
<script type="text/javascript">
function deleteItem() {
option = confirm('Are you sure to delete this item?')
if(!option) return false
return true
}
// function deleteItem(id) {
// option = confirm('Are you sure to delete this item?')
// if(!option) return
// $.post('delete-gift.php', {
// 'id': id
// }, function(data) {
// location.reload()
// })
// }
</script>
</body>
</html>
#delete-gift.php
<?php
session_start();
require_once ('db/dbhelper.php');
if (!empty($_POST)) {
$id = $_POST['id'];
if ($id > 0) {
execute("delete from gift where id = $id");
}
}
header('Location: gift.php');
#dbhelper.php
<?php
require_once ('config.php');
/**
* Su dung cho cac lenh: insert, update, delete
*/
function init() {
//Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD);
mysqli_set_charset($conn, 'utf8');
//query
$sql = 'create database if not exists gift_db';
mysqli_query($conn, $sql);
//Dong ket noi
mysqli_close($conn);
}
/**
* Su dung cho cac lenh: insert, update, delete
*/
function execute($sql) {
//Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
//query
mysqli_query($conn, $sql);
//Dong ket noi
mysqli_close($conn);
}
/**
* Su dung cho cac lenh: select
*/
function executeResult($sql) {
//Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
//query
$resultset = mysqli_query($conn, $sql);
$data = [];
while (($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row;
}
//Dong ket noi
mysqli_close($conn);
return $data;
}
#config.php
<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'gift_db');
#add-gift.php
<?php
session_start();
require_once ('db/dbhelper.php');
if (!empty($_POST)) {
$title = $_POST['title'];
$thumbnail = $_POST['thumbnail'];
$price = $_POST['price'];
$content = $_POST['content'];
$updated_at = $created_at = date('Y-m-d H:i:s');
$id = $_POST['id'];
if (!empty($title)) {
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, content, thumbnail, price, updated_at, created_at) values ('$title', '$content', '$thumbnail', '$price', '$updated_at', '$created_at')";
}
execute($sql);
header("Location: gift.php");
die();
}
}
$id = '';
$title = $thumbnail = $content = $price = '';
if (isset($_GET['id'])) {
$id = $_GET['id'];
$data = executeResult('select * from gift where id = '.$id);
if (count($data) > 0) {
$title = $data[0]['title'];
$thumbnail = $data[0]['thumbnail'];
$content = $data[0]['content'];
$price = $data[0]['price'];
}
}
?>
<html>
<head>
<title>Editor gift page</title>
<meta charset="utf-8">
<style type="text/css">
.main-content {
width: 640px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="main-content">
<h2 style="text-align: center;">Editor</h2>
<form method="post">
<label>Title: </label>
<input type="text" name="title" placeholder="Enter title" required="true" style="width: 100%;" value="<?=$title?>">
<input type="text" name="id" value="<?=$id?>" hidden>
<br/><br/>
<label>Thumbnail: </label>
<input type="text" name="thumbnail" placeholder="Enter thumbnail" required="true" style="width: 100%;" value="<?=$thumbnail?>">
<br/><br/>
<label>Price: </label>
<input type="number" name="price" placeholder="Enter price" required="true" style="width: 100%;" value="<?=$price?>">
<br/><br/>
<label>Content: </label>
<textarea name="content" placeholder="Enter content" required="true" rows="5" style="width: 100%;"><?=$content?></textarea>
<br/><br/>
<button type="submit">Save</button>
</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)