By GokiSoft.com| 09:37 21/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 - C2010G


#config.php


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


#dbhelper.php


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

/**
* Su dung cho cac lenh: insert, update, delete
*/
function initDB($sql) {
	//Mo ket noi toi database
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD);
	mysqli_set_charset($conn, 'utf8');

	//query
	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, $onlyOne = false) {
	//Mo ket noi toi database
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	//query
	$resultset = mysqli_query($conn, $sql);

	if($onlyOne) {
		$data = mysqli_fetch_array($resultset, 1);
	} else {
		$data = [];
		while(($row = mysqli_fetch_array($resultset, 1)) != null) {
			$data[] = $row;
		}
	}
	//Dong ket noi
	mysqli_close($conn);

	return $data;
}


#add-note.php


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

require_once('db/dbhelper.php');
require_once('form-note.php');
?>

<!DOCTYPE html>
<html>
<head>
	<title>Note page</title>
	<meta charset="utf-8">

	<style type="text/css">
		input, textarea {
			width: 90%;
		}
	</style>
</head>
<body>
	<h1 style="text-align: center;">Note Online</h1>
	<div style="margin: 0px auto; width: 600px; background-color: #f3d375; padding: 10px;">
		<form method="post">
			<p>Title: </p>
			<input type="text" name="title" required="true" class="Enter title">
			<p>Note: </p>
			<textarea rows="5" name="content"></textarea>
			<p>
				<button type="submit">Save</button>
			</p>
		</form>
	</div>
</body>
</html>


#form-login.php


<?php
$email = $password  = '';

if(!empty($_POST)) {
	$email = $_POST['email'];
	$password = $_POST['password'];

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

		if($user != null) {
			//login thanh cong
			$_SESSION['user'] = $user;
			
			header('Location: notes.php');
			die();
		}
	}
}


#form-note.php


<?php
$title = $content = '';

if(!empty($_POST)) {
	$title = $_POST['title'];
	$content = $_POST['content'];
	$created_at = $updated_at = date('Y-m-d H:i:s');
	$user_id = $_SESSION['user']['id'];

	if($title != '') {
		$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();
	}
}


#form-signup.php


<?php
$fullname = $email = $password = $confirmPwd = $birthday = $address = '';

if(!empty($_POST)) {
	$fullname = $_POST['fullname'];
	$email = $_POST['email'];
	$password = $_POST['password'];
	$confirmPwd = $_POST['confirm_pwd'];
	$birthday = $_POST['birthday'];
	$address = $_POST['address'];

	if($password == $confirmPwd && $password != '' && $email != '') {
		$sql = "insert into users(fullname, email, password, birthday, address) values ('$fullname', '$email', '$password', '$birthday', '$address')";
		execute($sql);

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


#init.php


<?php
require_once('db/dbhelper.php');

if(!empty($_POST)) {
	$sql = "create database if not exists ".DATABASE;
	initDB($sql);

	$sql = "create table if not exists users (
		id int primary key auto_increment,
		fullname varchar(150) not null,
		email varchar(250) unique,
		birthday date,
		address varchar(200),
		password varchar(32)	
	)";
	execute($sql);

	$sql = "create table if not exists notes (
		id int primary key auto_increment,
		user_id int references users (id),
		title varchar(250),
		content longtext,
		created_at datetime,
		updated_at datetime
	)";
	execute($sql);
}
?>

<!DOCTYPE html>
<html>
<head>
	<title>Init Database</title>
	<meta charset="utf-8">
</head>
<body>
	<h1 style="text-align: center;">Init DataBase</h1>
	<h3 style="text-align: center;">
		<form method="post">
			<button name="action" value="init_database">Start Init DataBase</button>
		</form>
	</h3>
</body>
</html>


#login.php


<?php
session_start();

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

require_once('db/dbhelper.php');
require_once('form-login.php');
?>

<!DOCTYPE html>
<html>
<head>
	<title>Login page</title>
	<meta charset="utf-8">

	<style type="text/css">
		input {
			width: 90%;
		}
	</style>
</head>
<body>
	<h1 style="text-align: center;">Login Up</h1>
	<div style="margin: 0px auto; width: 600px; background-color: #f3d375; padding: 10px;">
		<form method="post">
			<p>Email: </p>
			<input type="email" name="email" required="true" class="Enter email">
			<p>Password: </p>
			<input type="password" name="password" required="true" class="Enter password">
			<p>
				<button type="submit">Register</button>
			</p>
		</form>
	</div>
</body>
</html>


#notes.php


<?php
session_start();

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

require_once('db/dbhelper.php');
$noteList = executeResult('select * from notes');
?>
<!DOCTYPE html>
<html>
<head>
	<title>Note Web - Online</title>
	<meta charset="utf-8">
</head>
<body>
	<h1 style="text-align: center;">Note Online</h1>
	<div style="margin: 0px auto; width: 600px; background-color: #f3d375; padding: 10px;">
		<a href="add-note.php"><button style="margin-bottom: 10px;">Add Note</button></a>
		<table border="1" cellspacing="0" cellpadding="3" style="width: 100%;">
			<tr>
				<th>No</th>
				<th>Title</th>
				<th>Updated At</th>
				<th></th>
				<th></th>
			</tr>
<?php
$count = 0;
foreach ($noteList as $item) {
	echo '<tr>
			<td>'.(++$count).'</td>
			<td>'.$item['title'].'</td>
			<td>'.$item['updated_at'].'</td>
			<td><button>Edit</button></td>
			<td><button>Delete</button></td>
		</tr>';
}
?>
		</table>
	</div>
</body>
</html>


#readme.txt


B1. Phan tich database
1) Tao database
- create database if not exists BT2299
2) Tao tables
create table if not exists users (
	id int primary key auto_increment,
	fullname varchar(150) not null,
	email varchar(250) unique,
	birthday date,
	address varchar(200),
	password varchar(32)
)

create table if not exists notes (
	id int primary key auto_increment,
	user_id int references users (id),
	title varchar(250),
	content longtext,
	created_at datetime,
	updated_at datetime
)

B2. Xay dung khung chuong trinh
- Thu vien -> db
				- config.php
				- dbhelper.php

B2. Phan tich du an
1) init.php
2) signup.php


#signup.php


<?php
session_start();
if(isset($_SESSION['user'])) {
	header('Location: notes.php');
	die();
}

require_once('db/dbhelper.php');
require_once('form-signup.php');
?>

<!DOCTYPE html>
<html>
<head>
	<title>Signup page</title>
	<meta charset="utf-8">

	<style type="text/css">
		input {
			width: 90%;
		}
	</style>
</head>
<body>
	<h1 style="text-align: center;">Sign Up</h1>
	<div style="margin: 0px auto; width: 600px; background-color: #f3d375; padding: 10px;">
		<form method="post">
			<p>Fullname: </p>
			<input type="text" name="fullname" required="true" class="Enter fullname">
			<p>Email: </p>
			<input type="email" name="email" required="true" class="Enter email">
			<p>Birthday: </p>
			<input type="date" name="birthday" required="true" class="Enter birthday">
			<p>Address: </p>
			<input type="text" name="address" required="true" class="Enter address">
			<p>Password: </p>
			<input type="password" name="password" required="true" class="Enter password">
			<p>Confirm password: </p>
			<input type="password" name="confirm_pwd" required="true" class="Enter confirm password">
			<p>
				<button type="submit">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)

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

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