By GokiSoft.com|
20:36 24/09/2020|
Học PHP
[Share Code] Hướng dẫn chữa bài tập quản lý sinh viên - Lập trình PHP MySQL
Viết website quản lý sinh viên PHP & MySQL - Lập Trình PHP
#config.php
<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'C2002L');
#DBHelper.php
<?php
require_once ('config.php');
function execute($sql) {
//save data into table
// open connection to database
$con = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
//insert, update, delete
mysqli_query($con, $sql);
//close connection
mysqli_close($con);
}
function executeResult($sql) {
//save data into table
// open connection to database
$con = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
//insert, update, delete
$result = mysqli_query($con, $sql);
$data = [];
while ($row = mysqli_fetch_array($result, 1)) {
$data[] = $row;
}
//close connection
mysqli_close($con);
return $data;
}
#list.php
<?php
require_once ('DBHelper.php');
if (!empty($_POST)) {
$action = $id = '';
if (isset($_POST['action'])) {
$action = $_POST['action'];
}
if (isset($_POST['id'])) {
$id = $_POST['id'];
}
if ($action == 'delete') {
$sql = 'delete from student where id = '.$id;
execute($sql);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Student List</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">Student List</h2>
</div>
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th>STT</th>
<th>Full Name</th>
<th>Age</th>
<th>Address</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$sql = 'select * from student';
$data = executeResult($sql);
$index = 1;
foreach ($data as $row) {
echo '<tr>
<td>'.($index++).'</td>
<td>'.$row['fullname'].'</td>
<td>'.$row['age'].'</td>
<td>'.$row['address'].'</td>
<td><a href="register.php?id='.$row['id'].'"><button class="btn btn-warning">Edit</button></a></td>
<td><button class="btn btn-danger" data-toggle="modal" data-target="#myModal" onclick="saveId('.$row['id'].')">Delete</button></td>
</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>
<!-- modal -->
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Thông Báo</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Bạn chắc chắn muốn xoá sinh viên này không?
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal" onclick="deleteStudent()">Delete Now</button>
<button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- modal stop -->
<script type="text/javascript">
var selectId = '';
function saveId(id) {
selectId = id;
}
function deleteStudent() {
// option = confirm('Bạn chắc chắn muốn xoá sinh viên này không?')
// if(!option) {
// return;
// }
$.post('list.php', {
'action': 'delete',
'id': selectId
}, function(data) {
location.reload()
})
}
</script>
</body>
</html>
#register.php
<?php
require_once ('DBHelper.php');
$fullname = $age = $address = '';
$id = '';
// var_dump($_POST);
if (!empty($_POST)) {
if (isset($_POST['fullname'])) {
$fullname = $_POST['fullname'];
}
if (isset($_POST['age'])) {
$age = $_POST['age'];
}
if (isset($_POST['address'])) {
$address = $_POST['address'];
}
if (isset($_POST['id'])) {
$id = $_POST['id'];
}
//query
if (!empty($id)) {
$sql = "update student set fullname = '$fullname', age = '$age', address = '$address' where id = " .$id;
} else {
$sql = "insert into student(fullname, age, address) values ('".$fullname."', '$age', '$address')";
}
// echo $sql;
execute($sql);
$fullname = $age = $address = '';
}
if (isset($_GET['id'])) {
//edit
$id = $_GET['id'];
$sql = 'select * from student where id = '.$id;
$data = executeResult($sql);
if ($data != null && count($data) > 0) {
$fullname = $data[0]['fullname'];
$age = $data[0]['age'];
$address = $data[0]['address'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Registation Form</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">Registation Form</h2>
</div>
<div class="panel-body">
<form method="post">
<div class="form-group">
<label for="fullname">Name:</label>
<input type="text" class="form-control" id="id" name="id" value="<?=$id?>" style="display: none;">
<input required="true" type="text" class="form-control" id="fullname" name="fullname" value="<?=$fullname?>">
</div>
<div class="form-group">
<label for="age">Age:</label>
<input required="true" type="number" class="form-control" id="age" name="age" value="<?=$age?>">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address" value="<?=$address?>">
</div>
<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)