By GokiSoft.com| 09:23 29/04/2022|
Học PHP

[Source Code] Quản lý sách bằng PHP - trang quản trị sách bằng PHP - Lập trình PHP - C2108G3

Quản lý sách bằng PHP - trang quản trị sách bằng PHP - Lập trình PHP


#add.php


<?php
session_start();

require_once('dbhelper.php');

if(!empty($_POST)) {
	$title = $_POST['title'];
	$author = $_POST['author'];
	$price = $_POST['price'];
	$nsx = $_POST['nsx'];

	$sql = "insert into book(title, author, price, nsx) values ('$title', '$author', '$price', '$nsx')";
	execute($sql);

	header('Location: books.php');
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Add Book Page</title>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<form method="post">
	<div class="form-group">
		<label>Title: </label>
		<input type="text" name="title" class="form-control">
	</div>
	<div class="form-group">
		<label>Author: </label>
		<input type="text" name="author" 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>NSX: </label>
		<input type="text" name="nsx" class="form-control">
	</div>
	<div class="form-group">
		<button class="btn btn-success">Save</button>
	</div>
</form>
</body>
</html>


#books.php


<?php
session_start();

require_once('dbhelper.php');

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

$sql = "select * from book";
$bookList = executeResult($sql);
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Books Page</title>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<a href="add.php"><button>Add Book</button></a>
<table border="1" cellpadding="5">
	<thead>
		<tr>
			<th>No</th>
			<th>Title</th>
			<th>Author</th>
			<th>Price</th>
			<th>NSX</th>
			<th></th>
			<th></th>
		</tr>
	</thead>
	<tbody>
<?php
$index = 0;
foreach($bookList as $item) {
	echo '<tr>
			<td>'.(++$index).'</td>
			<td>'.$item['title'].'</td>
			<td>'.$item['author'].'</td>
			<td>'.$item['price'].'</td>
			<td>'.$item['nsx'].'</td>
			<td>
				<a href="edit.php?id='.$item['id'].'"><button>Edit</button></a>
			</td>
			<td>
				<a href="delete.php?id='.$item['id'].'"><button>Delete</button></a>
			</td>
		</tr>';
}
?>
	</tbody>
</table>
</body>
</html>


#config.php


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

const SQL_CREATE_DATABASE = 'create database if not exists '.DATABASE;

const SQL_CREATE_TABLE_USER = 'create table if not exists users (
	id int primary key auto_increment,
	username varchar(50),
	email varchar(150),
	fullname varchar(50),
	phone_number varchar(20),
	password varchar(32)
)';

const SQL_CREATE_TABLE_BOOK = 'create table if not exists book (
	id int primary key auto_increment,
	title varchar(250),
	author varchar(50),
	price float,
	nsx varchar(50)
)';


#dbhelper.php


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

function execute($sql) {
	// B1) Ket noi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	// B2) Insert du lieu vao database
	// echo $sql;die();
	// insert, update, delete
	mysqli_query($conn, $sql);

	// B3) Dong ket noi CSDL
	mysqli_close($conn);
}

function executeResult($sql, $isSingle = false) {
	// B1) Ket noi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	// B2) Insert du lieu vao database
	// echo $sql;die();
	// insert, update, delete
	$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) Dong ket noi CSDL
	mysqli_close($conn);

	return $data;
}


#delete.php


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

$id = $_GET['id'];

$sql = "delete from book where id = $id";
execute($sql);

header('Location: books.php');


#edit.php


<?php
session_start();

require_once('dbhelper.php');

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

	$sql = "update book set title = '$title', author = '$author', price = '$price', nsx = '$nsx' where id = $id";
	execute($sql);

	header('Location: books.php');
}

$id = $_GET['id'];
$sql = "select * from book where id = $id";
$book = executeResult($sql, true);
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Add Book Page</title>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<form method="post">
	<div class="form-group">
		<label>Title: </label>
		<input type="text" name="title" class="form-control" value="<?=$book['title']?>">
		<input type="text" name="id" class="form-control" value="<?=$book['id']?>" style="display: none;">
	</div>
	<div class="form-group">
		<label>Author: </label>
		<input type="text" name="author" class="form-control" value="<?=$book['author']?>">
	</div>
	<div class="form-group">
		<label>Price: </label>
		<input type="number" name="price" class="form-control" value="<?=$book['price']?>">
	</div>
	<div class="form-group">
		<label>NSX: </label>
		<input type="text" name="nsx" class="form-control" value="<?=$book['nsx']?>">
	</div>
	<div class="form-group">
		<button class="btn btn-success">Save</button>
	</div>
</form>
</body>
</html>


#login.php


<?php
session_start();

require_once('dbhelper.php');

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) {
		$_SESSION['cUser'] = $user;
		
		header('Location: books.php');
		die();
	}
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>SignUp Page</title>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<form method="post">
	<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="signup.php">Create a new account</a>
		</p>
		<button class="btn btn-success">Register</button>
	</div>
</form>
</body>
</html>


#signup.php


<?php
session_start();

require_once('dbhelper.php');

if(!empty($_POST)) {
	$username = $_POST['username'];
	$email = $_POST['email'];
	$fullname = $_POST['fullname'];
	$phone = $_POST['phone'];
	$pwd = $_POST['pwd'];

	$sql = "insert into users(username, email, fullname, phone_number, password) values ('$username', '$email', '$fullname', '$phone', '$pwd')";
	execute($sql);
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>SignUp Page</title>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<form method="post">
	<div class="form-group">
		<label>User Name: </label>
		<input type="text" name="username" 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>Full Name: </label>
		<input type="text" name="fullname" class="form-control">
	</div>
	<div class="form-group">
		<label>Phone Number: </label>
		<input type="tel" name="phone" 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="login.php">I have a account</a>
		</p>
		<button class="btn btn-success">Register</button>
	</div>
</form>
</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 đó