By GokiSoft.com| 11:22 05/05/2021|
Học PHP

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

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


#books.php


<?php
require_once('dbhelper.php');
?>
<!DOCTYPE html>
<html>
<head>
	<title>Book Management</title>
	<meta charset="utf-8">
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Book Management</h2>
			</div>
			<div class="panel-body">
				<button class="btn btn-success" style="margin-bottom: 10px;">Add new book</button>
				<table class="table table-bordered">
					<thead>
						<tr>
							<th>No</th>
							<th>Title</th>
							<th>Author Name</th>
							<th>Price</th>
							<th>NXB</th>
							<th style="width: 50px;"></th>
							<th style="width: 50px;"></th>
						</tr>
					</thead>
					<tbody>
<?php
	$num_page = 6;
	$page = 1;
	if(isset($_GET['page'])) {
		$page = $_GET['page'];
	}
	$index = ($page - 1) * $num_page;

	$sql = 'select count(*) total from books';
	$bookList = executeResult($sql);
	$total = $bookList[0]['total'];

	$totalPage = ceil($total/$num_page);

	$sql = 'select * from books limit '.$index.', '.$num_page;
	$bookList = executeResult($sql);

	$count = $index;
	foreach ($bookList as $item) {
		echo "<tr>
				<td>".(++$count)."</td>
				<td>".$item['title']."</td>
				<td>".$item['authorname']."</td>
				<td>".$item['price']."</td>
				<td>".$item['nxb']."</td>
				<td><button class='btn btn-warning'>Edit</button></td>
				<td><button class='btn btn-danger'>Delete</button></td>
			</tr>";
	}
?>
					</tbody>
				</table>
				<ul class="pagination">
					<?php
						if($page > 1) {
							echo '<li class="page-item"><a class="page-link" href="?page='.($page - 1).'">Previous</a></li>';
						}

						$pageList = [1, $page - 1, $page, $page + 1, $totalPage];

						$isFirst = $isBefore = false;
						for ($i=1; $i <= $totalPage; $i++) {
							if(!in_array($i, $pageList)) {
								if(!$isFirst && $i < $page) {
									$isFirst = true;
									echo '<li class="page-item"><a class="page-link" href="?page='.($page - 2).'">...</a></li>';
								}
								if(!$isBefore && $i > ($page+1)) {
									$isBefore = true;
									echo '<li class="page-item"><a class="page-link" href="?page='.($page + 2).'">...</a></li>';
								}
								continue;
							}
							if($i == $page) {
								echo '<li class="page-item active"><a class="page-link" href="?page='.$i.'">'.$i.'</a></li>';
							} else {
								echo '<li class="page-item"><a class="page-link" href="?page='.$i.'">'.$i.'</a></li>';
							}
						}
						if($page < $totalPage) {
							echo '<li class="page-item"><a class="page-link" href="?page='.($page + 1).'">Next</a></li>';
						}
					?>
				</ul>
			</div>
		</div>
	</div>
</body>
</html>


#config.php


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

define('MD5_PRIMARY_KEY', 'UJsdhjskdhfkIOU4235jkhsdUJH234dhdhfdhhj');
//md5(md5(password) + MD5_PRIMARY_KEY) -> ma hoa kha an toan
//Vi du: password = 123456
//md5 -> e10adc3949ba59abbe56e057f20f883e
//new string: md5(123456) + MD5_PRIMARY_KEY = e10adc3949ba59abbe56e057f20f883eUJsdhjskdhfkIOU4235jkhsdUJH234dhdhfdhhj
//md5 -> 5d002841726bb8b622dc1ca161119c2a


#dbhelper.php


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

/**
* 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;
}


#login.php


<?php
require_once("login_form.php");
?>

<!DOCTYPE html>
<html>
<head>
	<title>Login - Gokisoft</title>
	<meta charset="utf-8">
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Login</h2>
			</div>
			<div class="panel-body">
				<form method="post">
					<div class="form-group">
					  <label for="email">Email:</label>
					  <input required="true" type="email" class="form-control" id="email" name="email">
					</div>
					<div class="form-group">
					  <label for="pwd">Password:</label>
					  <input required="true" type="password" class="form-control" id="pwd" name="password">
					</div>
					<p><a href="signup.php">Register new user</a></p>
					<button class="btn btn-success">Login</button>
				</form>
			</div>
		</div>
	</div>
</body>
</html>


#login_form.php


<?php
require_once('dbhelper.php');
if(!empty($_POST)) {
	$email = $password = '';

	if(isset($_POST['email'])) {
		$email = $_POST['email'];
	}

	if(isset($_POST['password'])) {
		$password = $_POST['password'];
		$password = md5(md5($password).MD5_PRIMARY_KEY);
		// echo $password;die();
	}

	//check tai khoan co ton tai trong database
	$sql = "select * from users where email = '$email' and password = '$password'";
	$result = executeResult($sql);
	// var_dump($result);
	if($result != null && sizeof($result) == 1) {
		//login thanh cong
		header('Location: books.php');
		die();
	}
}


#readme.txt


B1. Tao tables trong database
create table users (
	id int primary key auto_increment,
	username varchar(50) unique,
	email varchar(150) unique,
	fullname varchar(50),
	phone_number varchar(20),
	password varchar(32)
)
//password: 123456 -> ma hoa -> md5 -> khong con bao mat nua -> md5 2 lop + private key.

create table books (
	id int primary key auto_increment,
	title varchar(200) not null,
	authorname varchar(50),
	price float,
	nxb varchar(150)
)

B2. Phat trien chuc nang chuong trinh


#register_form.php


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

$fullname = $username = $phone_number = $email = $password = $confirmation_pwd = '';
if(!empty($_POST)) {
	if(isset($_POST['fullname'])) {
		$fullname = $_POST['fullname'];
	}

	if(isset($_POST['username'])) {
		$username = $_POST['username'];
	}

	if(isset($_POST['phone_number'])) {
		$phone_number = $_POST['phone_number'];
	}

	if(isset($_POST['email'])) {
		$email = $_POST['email'];
	}

	if(isset($_POST['password'])) {
		$password = $_POST['password'];
	}

	if(isset($_POST['confirmation_pwd'])) {
		$confirmation_pwd = $_POST['confirmation_pwd'];
	}

	if($password == $confirmation_pwd) {
		//Kiem tra thanh cong mat khau da khop
		if(!empty($username) && !empty($email)) {
			//Kiem tra username & email <> null -> check bao mat phia server
			//Xem username va email da ton tai trong database
			$sql = "select * from users where username = '$username' or email = '$email'";
			$result = executeResult($sql);
			// var_dump($result);
			if($result != null && sizeof($result) > 0) {
				//Tai khoan da ton tai trong database
			} else {
				//Kiem tra moi thong tin da ok -> insert database
				// echo $password.'<br/>';
				$password = md5(md5($password).MD5_PRIMARY_KEY);
				// echo $password;die();

				$sql = "insert into users(username, email, fullname, phone_number, password) values ('$username', '$email', '$fullname', '$phone_number', '$password')";
				execute($sql);

				header('Location: login.php');
				die();
			}
		}
	}
	// var_dump($_POST);
}


#signup.php


<?php
require_once('register_form.php');
?>

<!DOCTYPE html>
<html>
<head>
	<title>Registation Form - Gokisoft</title>
	<meta charset="utf-8">
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Register</h2>
				<?php
					if(!empty($_POST)) {
						echo '<h2 style="color: red">User is existed</h2>';
					}
				?>
			</div>
			<div class="panel-body">
				<form method="post">
					<div class="form-group">
					  <label for="usr">Full Name:</label>
					  <input required="true" type="text" class="form-control" id="usr" name="fullname" value="<?=$fullname?>">
					</div>
					<div class="form-group">
					  <label for="usr">User Name:</label>
					  <input required="true" type="text" class="form-control" id="usr" name="username" value="<?=$username?>">
					</div>
					<div class="form-group">
					  <label for="email">Email:</label>
					  <input required="true" type="email" class="form-control" id="email" name="email" value="<?=$email?>">
					</div>
					<div class="form-group">
					  <label for="birthday">Phone Number:</label>
					  <input type="telno" class="form-control" id="phone" name="phone_number" value="<?=$phone_number?>">
					</div>
					<div class="form-group">
					  <label for="pwd">Password:</label>
					  <input required="true" type="password" class="form-control" id="pwd" name="password">
					</div>
					<div class="form-group">
					  <label for="confirmation_pwd">Confirmation Password:</label>
					  <input required="true" type="password" class="form-control" id="confirmation_pwd" name="confirmation_pwd">
					</div>
					<p><a href="login.php">I have a account (login)</a></p>
					<button class="btn btn-success">Register</button>
				</form>
			</div>
		</div>
	</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)

GokiSoft.com [Teacher]
GokiSoft.com

2021-05-05 04:21:59

Update Phân Trang


#books.php


<?php
require_once('dbhelper.php');
?>
<!DOCTYPE html>
<html>
<head>
	<title>Book Management</title>
	<meta charset="utf-8">
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Book Management</h2>
			</div>
			<div class="panel-body">
				<button class="btn btn-success" style="margin-bottom: 10px;">Add new book</button>
				<table class="table table-bordered">
					<thead>
						<tr>
							<th>No</th>
							<th>Title</th>
							<th>Author Name</th>
							<th>Price</th>
							<th>NXB</th>
							<th style="width: 50px;"></th>
							<th style="width: 50px;"></th>
						</tr>
					</thead>
					<tbody>
<?php
	$num_page = 6;
	$page = 1;
	if(isset($_GET['page'])) {
		$page = $_GET['page'];
	}
	$index = ($page - 1) * $num_page;

	$sql = 'select count(*) total from books';
	$bookList = executeResult($sql);
	$total = $bookList[0]['total'];

	$totalPage = ceil($total/$num_page);

	$sql = 'select * from books limit '.$index.', '.$num_page;
	$bookList = executeResult($sql);

	$count = $index;
	foreach ($bookList as $item) {
		echo "<tr>
				<td>".(++$count)."</td>
				<td>".$item['title']."</td>
				<td>".$item['authorname']."</td>
				<td>".$item['price']."</td>
				<td>".$item['nxb']."</td>
				<td><button class='btn btn-warning'>Edit</button></td>
				<td><button class='btn btn-danger'>Delete</button></td>
			</tr>";
	}
?>
					</tbody>
				</table>
				<ul class="pagination">
					<?php
						if($page > 1) {
							echo '<li class="page-item"><a class="page-link" href="?page='.($page - 1).'">Previous</a></li>';
						}

						$pageList = [1, $page - 1, $page, $page + 1, $totalPage];

						$isFirst = $isBefore = false;
						for ($i=1; $i <= $totalPage; $i++) {
							if(!in_array($i, $pageList)) {
								if(!$isFirst && $i < $page) {
									$isFirst = true;
									echo '<li class="page-item"><a class="page-link" href="?page='.($page - 2).'">...</a></li>';
								}
								if(!$isBefore && $i > ($page+1)) {
									$isBefore = true;
									echo '<li class="page-item"><a class="page-link" href="?page='.($page + 2).'">...</a></li>';
								}
								continue;
							}
							if($i == $page) {
								echo '<li class="page-item active"><a class="page-link" href="?page='.$i.'">'.$i.'</a></li>';
							} else {
								echo '<li class="page-item"><a class="page-link" href="?page='.$i.'">'.$i.'</a></li>';
							}
						}
						if($page < $totalPage) {
							echo '<li class="page-item"><a class="page-link" href="?page='.($page + 1).'">Next</a></li>';
						}
					?>
				</ul>
			</div>
		</div>
	</div>
</body>
</html>


#config.php


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

define('MD5_PRIMARY_KEY', 'UJsdhjskdhfkIOU4235jkhsdUJH234dhdhfdhhj');
//md5(md5(password) + MD5_PRIMARY_KEY) -> ma hoa kha an toan
//Vi du: password = 123456
//md5 -> e10adc3949ba59abbe56e057f20f883e
//new string: md5(123456) + MD5_PRIMARY_KEY = e10adc3949ba59abbe56e057f20f883eUJsdhjskdhfkIOU4235jkhsdUJH234dhdhfdhhj
//md5 -> 5d002841726bb8b622dc1ca161119c2a


#dbhelper.php


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

/**
* 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;
}


#login.php


<?php
require_once("login_form.php");
?>

<!DOCTYPE html>
<html>
<head>
	<title>Login - Gokisoft</title>
	<meta charset="utf-8">
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Login</h2>
			</div>
			<div class="panel-body">
				<form method="post">
					<div class="form-group">
					  <label for="email">Email:</label>
					  <input required="true" type="email" class="form-control" id="email" name="email">
					</div>
					<div class="form-group">
					  <label for="pwd">Password:</label>
					  <input required="true" type="password" class="form-control" id="pwd" name="password">
					</div>
					<p><a href="signup.php">Register new user</a></p>
					<button class="btn btn-success">Login</button>
				</form>
			</div>
		</div>
	</div>
</body>
</html>


#login_form.php


<?php
require_once('dbhelper.php');
if(!empty($_POST)) {
	$email = $password = '';

	if(isset($_POST['email'])) {
		$email = $_POST['email'];
	}

	if(isset($_POST['password'])) {
		$password = $_POST['password'];
		$password = md5(md5($password).MD5_PRIMARY_KEY);
		// echo $password;die();
	}

	//check tai khoan co ton tai trong database
	$sql = "select * from users where email = '$email' and password = '$password'";
	$result = executeResult($sql);
	// var_dump($result);
	if($result != null && sizeof($result) == 1) {
		//login thanh cong
		header('Location: books.php');
		die();
	}
}


#readme.txt


B1. Tao tables trong database
create table users (
	id int primary key auto_increment,
	username varchar(50) unique,
	email varchar(150) unique,
	fullname varchar(50),
	phone_number varchar(20),
	password varchar(32)
)
//password: 123456 -> ma hoa -> md5 -> khong con bao mat nua -> md5 2 lop + private key.

create table books (
	id int primary key auto_increment,
	title varchar(200) not null,
	authorname varchar(50),
	price float,
	nxb varchar(150)
)

B2. Phat trien chuc nang chuong trinh


#register_form.php


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

$fullname = $username = $phone_number = $email = $password = $confirmation_pwd = '';
if(!empty($_POST)) {
	if(isset($_POST['fullname'])) {
		$fullname = $_POST['fullname'];
	}

	if(isset($_POST['username'])) {
		$username = $_POST['username'];
	}

	if(isset($_POST['phone_number'])) {
		$phone_number = $_POST['phone_number'];
	}

	if(isset($_POST['email'])) {
		$email = $_POST['email'];
	}

	if(isset($_POST['password'])) {
		$password = $_POST['password'];
	}

	if(isset($_POST['confirmation_pwd'])) {
		$confirmation_pwd = $_POST['confirmation_pwd'];
	}

	if($password == $confirmation_pwd) {
		//Kiem tra thanh cong mat khau da khop
		if(!empty($username) && !empty($email)) {
			//Kiem tra username & email <> null -> check bao mat phia server
			//Xem username va email da ton tai trong database
			$sql = "select * from users where username = '$username' or email = '$email'";
			$result = executeResult($sql);
			// var_dump($result);
			if($result != null && sizeof($result) > 0) {
				//Tai khoan da ton tai trong database
			} else {
				//Kiem tra moi thong tin da ok -> insert database
				// echo $password.'<br/>';
				$password = md5(md5($password).MD5_PRIMARY_KEY);
				// echo $password;die();

				$sql = "insert into users(username, email, fullname, phone_number, password) values ('$username', '$email', '$fullname', '$phone_number', '$password')";
				execute($sql);

				header('Location: login.php');
				die();
			}
		}
	}
	// var_dump($_POST);
}


#signup.php


<?php
require_once('register_form.php');
?>

<!DOCTYPE html>
<html>
<head>
	<title>Registation Form - Gokisoft</title>
	<meta charset="utf-8">
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Register</h2>
				<?php
					if(!empty($_POST)) {
						echo '<h2 style="color: red">User is existed</h2>';
					}
				?>
			</div>
			<div class="panel-body">
				<form method="post">
					<div class="form-group">
					  <label for="usr">Full Name:</label>
					  <input required="true" type="text" class="form-control" id="usr" name="fullname" value="<?=$fullname?>">
					</div>
					<div class="form-group">
					  <label for="usr">User Name:</label>
					  <input required="true" type="text" class="form-control" id="usr" name="username" value="<?=$username?>">
					</div>
					<div class="form-group">
					  <label for="email">Email:</label>
					  <input required="true" type="email" class="form-control" id="email" name="email" value="<?=$email?>">
					</div>
					<div class="form-group">
					  <label for="birthday">Phone Number:</label>
					  <input type="telno" class="form-control" id="phone" name="phone_number" value="<?=$phone_number?>">
					</div>
					<div class="form-group">
					  <label for="pwd">Password:</label>
					  <input required="true" type="password" class="form-control" id="pwd" name="password">
					</div>
					<div class="form-group">
					  <label for="confirmation_pwd">Confirmation Password:</label>
					  <input required="true" type="password" class="form-control" id="confirmation_pwd" name="confirmation_pwd">
					</div>
					<p><a href="login.php">I have a account (login)</a></p>
					<button class="btn btn-success">Register</button>
				</form>
			</div>
		</div>
	</div>
</body>
</html>