By GokiSoft.com|
19:44 22/05/2021|
Học PHP
[Share Code] Bài tập - ứng dụng quản lý ghi chú - Lập trình PHP/MySQL - C2010L
#config.php
<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'bt2299');
#dbhelper.php
<?php
require_once ('config.php');
/**
* Su dung cho lenh: insert/update/delete
*/
function createDatabase() {
// Them du lieu vao database
//B1. Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD);
mysqli_set_charset($conn, 'utf8');
//B2. Thuc hien truy van insert
$sql = "create database if not exists ".DATABASE." DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci";
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);
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, $isSingle = false) {
// Them du lieu vao database
//B1. Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
//B2. Thuc hien truy van insert
$resultset = mysqli_query($conn, $sql);
$data = [];
if($isSingle) {
$data = mysqli_fetch_array($resultset, 1);
} else {
while (($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row;
}
}
//B3. Dong ket noi database
mysqli_close($conn);
return $data;
}
#add-note.php
<?php
session_start();
if(!isset($_SESSION['users'])) {
header("Location: login.php");
die();
}
require_once('db/dbhelper.php');
$title = $content = '';
if(!empty($_POST)) {
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$created_at = $updated_at = date('Y-m-d H:i:s');
$user_id = $_SESSION['users']['id'];
if($title != '') {
//register thanh cong
if($id > 0) {
$sql = "update notes set title = '$title', content = '$content', updated_at = '$updated_at' where id = $id";
} else {
$sql = "insert into notes (title, content, created_at, updated_at, user_id) values ('$title', '$content', '$created_at', '$updated_at', '$user_id')";
}
execute($sql);
header("Location: notes.php");
die();
}
}
$id = '';
if(isset($_GET['id'])) {
$id = $_GET['id'];
$note = executeResult('select * from notes where id = '.$id, true);
$title = $note['title'];
$content = $note['content'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>SignUp</title>
<meta charset="utf-8">
<style type="text/css">
input, textarea {
width: 90%;
}
</style>
</head>
<body>
<div style="width: 600px; margin: 0px auto; background-color: orange; padding: 10px;">
<form method="post">
<p>Title: </p>
<input required="true" type="text" name="title" value="<?=$title?>">
<input type="text" name="id" value="<?=$id?>" hidden="true">
<p>Content: </p>
<textarea name="content"><?=$content?></textarea>
<p>
<button>Register</button>
</p>
</form>
</div>
</body>
</html>
#guideline.txt
B1. Thiet ke database
1) Tao database
create database if not exists bt2299
2) Tao tables
create table users (
id int primary key auto_increment,
fullname varchar(200) not null,
email varchar(150) unique,
birthday date,
address varchar(200),
password varchar(32)
)
create table notes (
id int primary key auto_increment,
user_id int references users (id),
title varchar(300),
content longtext,
created_at datetime,
updated_at datetime
)
B2. Phat trien chuc nang du an
1) init.php
#init.php
<?php
session_start();
require_once('db/dbhelper.php');
if(!empty($_POST)) {
createDatabase();
$sql = "create table users (
id int primary key auto_increment,
fullname varchar(200) not null,
email varchar(150) unique,
birthday date,
address varchar(200),
password varchar(32)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
execute($sql);
$sql = "create table notes (
id int primary key auto_increment,
user_id int references users (id),
title varchar(300),
content longtext,
created_at datetime,
updated_at datetime
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
execute($sql);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Init Database - Page</title>
<meta charset="utf-8">
</head>
<body>
<?php
if(!empty($_POST)) {
echo '<h1 style="text-align: center; color: red">Init Database Completely</h1>';
} else {
echo '<h1 style="text-align: center;">Init Database</h1>';
}
?>
<div style="width: 200px; margin: 0px auto; text-align: center;">
<form method="post">
<button style="font-size: 20px" name="action" value="init database">Start Init Database</button>
</form>
</div>
</body>
</html>
#login.php
<?php
session_start();
if(isset($_SESSION['users'])) {
header("Location: notes.php");
die();
}
require_once('db/dbhelper.php');
$fullname = $email = $password = $confirm_password = $birthday = $address = '';
if(!empty($_POST)) {
$email = $_POST['email'];
$password = $_POST['password'];
if($password != '' && $email != '') {
//register thanh cong
$sql = "select * from users where email = '$email' and password = '$password'";
$user = executeResult($sql, true);
if($user != null) {
$_SESSION['users'] = $user;
header("Location: notes.php");
die();
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta charset="utf-8">
<style type="text/css">
input {
width: 90%;
}
</style>
</head>
<body>
<div style="width: 600px; margin: 0px auto; background-color: orange; padding: 10px;">
<form method="post">
<p>Email: </p>
<input required="true" type="email" name="email">
<p>Password: </p>
<input required="true" type="password" name="password">
<p>
<button>Login</button>
</p>
</form>
</div>
</body>
</html>
#notes.php
<?php
session_start();
if(!isset($_SESSION['users'])) {
header("Location: login.php");
die();
}
require_once('db/dbhelper.php');
if(!empty($_POST)) {
$id = $_POST['id'];
$sql = "delete from notes where id = $id";
execute($sql);
}
$sql = 'select * from notes where user_id = '.$_SESSION['users']['id'];
if(isset($_GET['s'])) {
$sql .= " and title like '%".$_GET['s']."%'";
}
$noteList = executeResult($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta charset="utf-8">
</head>
<body>
<div style="width: 600px; margin: 0px auto; padding: 10px;">
<div style="width: 100%; display: flex;">
<a href="add-note.php"><button style="margin-bottom: 10px; float: left;">Add new note</button></a>
<form method="get">
<input type="text" name="s" placeholder="Enter searching ..." style="width: 200px; margin-bottom: 10px;">
</form>
</div>
<table border="1" style="width: 100%;">
<tr>
<th>No</th>
<th>Title</th>
<th>Updated At</th>
<th width="50px"></th>
<th width="50px"></th>
</tr>
<?php
$count = 0;
foreach ($noteList as $item) {
echo '<tr>
<td>'.(++$count).'</td>
<td>'.$item['title'].'</td>
<td>'.$item['updated_at'].'</td>
<td><a href="add-note.php?id='.$item['id'].'"><button>Edit</button></a></td>
<td>
<form method="post" onsubmit="return confirmDelete();">
<input type="text" name="id" value="'.$item['id'].'" hidden="true"/>
<button>Delete</button>
</form>
</td>
</tr>';
}
?>
</table>
</div>
<script type="text/javascript">
function confirmDelete() {
option = confirm("Are you sure to delete this note???")
if(!option) return false
return true
}
</script>
</body>
</html>
#signup.php
<?php
session_start();
if(isset($_SESSION['users'])) {
header("Location: notes.php");
die();
}
require_once('db/dbhelper.php');
$fullname = $email = $password = $confirm_password = $birthday = $address = '';
if(!empty($_POST)) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
$birthday = $_POST['birthday'];
$address = $_POST['address'];
if($password == $confirm_password && $password != '' && $email != '') {
//register thanh cong
$sql = "insert into users (fullname, email, birthday, address, password) values ('$fullname', '$email', '$birthday', '$address', '$password')";
execute($sql);
header("Location: login.php");
die();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>SignUp</title>
<meta charset="utf-8">
<style type="text/css">
input {
width: 90%;
}
</style>
</head>
<body>
<div style="width: 600px; margin: 0px auto; background-color: orange; padding: 10px;">
<form method="post">
<p>Fullname: </p>
<input required="true" type="text" name="fullname">
<p>Email: </p>
<input required="true" type="email" name="email">
<p>Birthday: </p>
<input required="true" type="date" name="birthday">
<p>Password: </p>
<input required="true" type="password" name="password">
<p>Confirm Password: </p>
<input required="true" type="password" name="confirm_password">
<p>Address: </p>
<input required="true" type="text" name="address">
<p>
<button>Register</button>
</p>
</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)