By GokiSoft.com|
14:44 29/04/2022|
Học PHP
[Video] Quản lý sách bằng PHP - trang quản trị sách bằng PHP - Lập trình PHP - C2110I
https://gokisoft.com/quan-ly-sach-bang-php-trang-quan-tri-sach-bang-php-lap-trinh-php.html
#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 books(title, author, price, nsx) values ('$title', '$author', '$price', '$nsx')";
execute($sql);
header('Location: books.php');
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Register 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" placeholder="Enter title">
</div>
<div class="form-group">
<label>Author: </label>
<input type="text" name="author" class="form-control" placeholder="Enter author">
</div>
<div class="form-group">
<label>Price: </label>
<input type="number" name="price" class="form-control" placeholder="Enter price">
</div>
<div class="form-group">
<label>NSX: </label>
<input type="text" name="nsx" class="form-control" placeholder="Enter nsx">
</div>
<div class="form-group">
<button>Save</button>
</div>
</form>
</body>
</html>
#books.php
<?php
session_start();
if(!isset($_SESSION['cUser'])) {
header('Location: login.php');
die();
}
require_once('dbhelper.php');
if(isset($_GET['s'])) {
$s = $_GET['s'];
$sql = "select * from books where title like '%$s%'";
} else {
$sql = "select * from books";
}
$bookList = executeResult($sql);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Book Page</title>
</head>
<body>
<a href="add.php"><button>Add book</button></a>
<form method="get">
<input type="text" name="s" placeholder="Enter search ...">
</form>
<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_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 books (
id int primary key auto_increment,
title varchar(150),
author varchar(50),
price float,
nsx varchar(50)
)';
#dbhelper.php
<?php
require_once('config.php');
function execute($sql) {
// B1) Tao ket noi toi CSDL
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
// B2) Thuc hien cau insert into
// echo $sql;
// die();
// insert, update, delete
mysqli_query($conn, $sql);
// B3) Dong ket noi
mysqli_close($conn);
}
function executeResult($sql, $isSingle = false) {
// B1) Tao ket noi toi CSDL
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
// B2) Thuc hien cau insert into
$resultset = mysqli_query($conn, $sql);
$data = null;
if($isSingle) {
$data = mysqli_fetch_array($resultset, 1);
} else {
$data = [];
while(($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row; //$row -> array key & value
}
}
// B3) Dong ket noi
mysqli_close($conn);
return $data;
}
#delete.php
<?php
require_once('dbhelper.php');
$id = $_GET['id'];
$sql = "delete from books 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 books set title = '$title', author = '$author', price = '$price', nsx = '$nsx' where id = $id";
execute($sql);
header('Location: books.php');
die();
}
$id = $_GET['id'];
$sql = "select * from books 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>Register 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" placeholder="Enter title" value="<?=$book['title']?>">
<input type="text" name="id" class="form-control" placeholder="Enter id" value="<?=$book['id']?>" style="display: none;">
</div>
<div class="form-group">
<label>Author: </label>
<input type="text" name="author" class="form-control" placeholder="Enter author" value="<?=$book['author']?>">
</div>
<div class="form-group">
<label>Price: </label>
<input type="number" name="price" class="form-control" placeholder="Enter price" value="<?=$book['price']?>">
</div>
<div class="form-group">
<label>NSX: </label>
<input type="text" name="nsx" class="form-control" placeholder="Enter nsx" value="<?=$book['nsx']?>">
</div>
<div class="form-group">
<button>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) {
//login thanh cong
$_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>Register 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" placeholder="Enter email">
</div>
<div class="form-group">
<label>Password: </label>
<input type="password" name="pwd" class="form-control" placeholder="Enter password">
</div>
<div class="form-group">
<p>
<a href="signup.php">Create a new account</a>
</p>
<button>Login</button>
</div>
</form>
</body>
</html>
#readme.txt
- Xây dựng cấu trúc dự án:
- config.php
- dbhelper.php
-
#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>Register 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" placeholder="Enter user name">
</div>
<div class="form-group">
<label>Email: </label>
<input type="email" name="email" class="form-control" placeholder="Enter email">
</div>
<div class="form-group">
<label>Full Name: </label>
<input type="text" name="fullname" class="form-control" placeholder="Enter full name">
</div>
<div class="form-group">
<label>Phone: </label>
<input type="tel" name="phone" class="form-control" placeholder="Enter phone">
</div>
<div class="form-group">
<label>Password: </label>
<input type="password" name="pwd" class="form-control" placeholder="Enter password">
</div>
<div class="form-group">
<p>
<a href="login.php">I have a account</a>
</p>
<button>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)