By GokiSoft.com|
19:50 23/11/2022|
Học PHP
[Source Code] Tìm hiểu về CSDL - C2206L
#add.php
<?php
if(!empty($_POST)) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$birthday = $_POST['birthday'];
$address = $_POST['address'];
//Xu ly nghiep vu -> day thong tin nay vao database quan ly
//B1. Mo ket noi toi CSDL
$conn = mysqli_connect('localhost', 'root', '', 'C2206L');
mysqli_set_charset($conn, 'utf8');
//B2. Them/sua/xoa/lay du lieu tu database -> insert/update/delete/select
$sql = "insert into students(fullname, email, birthday, address) values ('$fullname', '$email', '$birthday', '$address')";
//echo $sql; -> ' -> \'
mysqli_query($conn, $sql);
//B3. Dong ket noi toi CSDL
mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add new student - Page</title>
<!-- 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">Add new student</h2>
</div>
<div class="panel-body">
<form method="post">
<div class="form-group">
<label for="usr">Name:</label>
<input required="true" type="text" class="form-control" id="usr" name="fullname" value="">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input required="true" type="email" class="form-control" id="email" name="email" value="">
</div>
<div class="form-group">
<label for="birthday">Birthday:</label>
<input type="date" class="form-control" id="birthday" name="birthday" value="">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address" value="">
</div>
<button type="submit" class="btn btn-success">Save Data</button>
<p>
<a href="show.php">Back to list</a>
</p>
</form>
</div>
</div>
</div>
</body>
</html>
#delete.php
<?php
if(isset($_GET['id'])) {
$id = $_GET['id'];
//B1. Mo ket noi toi CSDL
$conn = mysqli_connect('localhost', 'root', '', 'C2206L');
mysqli_set_charset($conn, 'utf8');
//B2. Them/sua/xoa/lay du lieu tu database -> insert/update/delete/select
$sql = "delete from students where id = $id";
mysqli_query($conn, $sql);
//B3. Dong ket noi toi CSDL
mysqli_close($conn);
}
header('Location: show.php');
#edit.php
<?php
$id = $fullname = $email = $birthday = $address = "";
if(!empty($_POST)) {
$id = $_POST['id'];
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$birthday = $_POST['birthday'];
$address = $_POST['address'];
//B1. Mo ket noi toi CSDL
$conn = mysqli_connect('localhost', 'root', '', 'C2206L');
mysqli_set_charset($conn, 'utf8');
//B2. Them/sua/xoa/lay du lieu tu database -> insert/update/delete/select
$sql = "update students set fullname = '$fullname', email = '$email', birthday = '$birthday', address = '$address' where id = $id";
mysqli_query($conn, $sql);
//B3. Dong ket noi toi CSDL
mysqli_close($conn);
header('Location: show.php');
}
if(isset($_GET['id'])) {
$id = $_GET['id'];
//B1. Mo ket noi toi CSDL
$conn = mysqli_connect('localhost', 'root', '', 'C2206L');
mysqli_set_charset($conn, 'utf8');
//B2. Them/sua/xoa/lay du lieu tu database -> insert/update/delete/select
$sql = "select * from students where id = $id";
$resultset = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($resultset, 1);
if($row != null) {
$id = $row['id'];
$fullname = $row['fullname'];
$email = $row['email'];
$birthday = $row['birthday'];
$address = $row['address'];
}
//B3. Dong ket noi toi CSDL
mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit new student - Page</title>
<!-- 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">Edit new student</h2>
</div>
<div class="panel-body">
<form method="post">
<div class="form-group">
<label for="usr">Name:</label>
<input required="true" type="text" class="form-control" id="id" name="id" value="<?=$id?>" style="display: none;">
<input required="true" type="text" class="form-control" id="usr" name="fullname" value="<?=$fullname?>">
</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">Birthday:</label>
<input type="date" class="form-control" id="birthday" name="birthday" value="<?=$birthday?>">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address" value="<?=$address?>">
</div>
<button type="submit" class="btn btn-warning">Save Data</button>
<p>
<a href="show.php">Back to list</a>
</p>
</form>
</div>
</div>
</div>
</body>
</html>
#readme.txt
Nội dung kiến thức trong buổi học hôm này
- Chữa bài tập:
- GET/POST trong PHP
- Kết nối CSDL:
- Môi trường phpmyadmin (mysql)
- Ôn lại 1 chút về CSDL:
Tạo tables
fake 1/2 bản ghi
- Chức năng: CRUD
Thêm -> DONE
Sửa -> Tự nghiên cưu
Xoá
Hiển thị danh sách sinh viên ->
========================================================
1) Thiet ke database
- tao CSDL -> C2206L
- tao table
create table students (
id int primary key auto_increment,
fullname varchar(50),
email varchar(150),
birthday date,
address varchar(200)
)
2) Trien khai code:
-
#show.php
<?php
//B1. Mo ket noi toi CSDL
$conn = mysqli_connect('localhost', 'root', '', 'C2206L');
mysqli_set_charset($conn, 'utf8');
//B2. Them/sua/xoa/lay du lieu tu database -> insert/update/delete/select
$sql = "select * from students";
$resultset = mysqli_query($conn, $sql);
$data = [];
while(($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row;
}
//B3. Dong ket noi toi CSDL
mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<title>Show student - Page</title>
<!-- 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">
<a href="add.php"><button class="btn btn-success mb-2 mt-2">Add new student</button></a>
<div class="panel panel-primary">
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Full Name</th>
<th>Email</th>
<th>Birthday</th>
<th>Address</th>
<th style="width: 190px"></th>
</tr>
</thead>
<tbody>
<?php
$count = 0;
foreach ($data as $item) {
echo '<tr>
<td>'.++$count.'</td>
<td>'.$item['fullname'].'</td>
<td>'.$item['email'].'</td>
<td>'.$item['birthday'].'</td>
<td>'.$item['address'].'</td>
<td>
<a href="edit.php?id='.$item['id'].'"><button class="btn btn-warning">Edit</button></a>
<a href="delete.php?id='.$item['id'].'"><button class="btn btn-danger">Remove</button></a>
</td>
</tr>';
}
?>
</tbody>
</table>
</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)