ຈັດການພະແນກ (ເພີ່ມ, ລົບ, ແກ້ໄຂ)
Code ປະກອບໃນການຮຽນ
ສ້າງໄຟລ໌ department.php
<?php
session_start();
if (!isset($_SESSION['admin_login'])) {
header("location: ../index.php");
}
?><section class="content">
<div class="container-fluid">
<!-- Small boxes (Stat box) -->
<a href="department.php?act=add" class="btn btn-success mb-2">
<i class="fas fa-plus"></i> ເພີ່ມຂໍ້ມູນ</a>
<!-- ./col -->
<div class="col-md-12">
<?php
$act = (isset($_GET['act']) ? $_GET['act'] : '');
if ($act == 'add') {
include('department_add.php');
}elseif($act == 'edit'){
include('department_edit.php');
}else{
include('department_list.php');
}
?>
</div>
</div>
<!-- /.row -->
</div><!-- /.container-fluid -->
</section>
<!-- /.content -->
ສ້າງໄຟລ໌ department_list.php ໜ້າສະແດງລາຍການພະແນກ
<?php
include 'condb.php';
$stmt = $conn->prepare("SELECT* FROM tb_department");
$stmt->execute();
$result = $stmt->fetchAll();
?>
<table id="example1" class="table table-bordered table-striped dataTable">
<thead>
<tr role="row" class="info">
<th tabindex="0" rowspan="1" colspan="1" >ລຳດັບ</th>
<th tabindex="0" rowspan="1" colspan="1" >ຊື່ພະແນກ</th>
<th tabindex="0" rowspan="1" colspan="1" >ແກ້ໄຂ</th>
<th tabindex="0" rowspan="1" colspan="1" >ລຶບ</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row_dm) { ?>
<tr>
<td>
<?php echo $row_dm['department_id']; ?>
</td>
<td>
<?php echo $row_dm['department_name']; ?>
</td>
<td>
<a class="btn btn-warning btn-flat btn-sm" href="department.php?act=edit&department_id=<?php echo $row_dm['department_id']; ?>">
<i class="nav-icon fas fa-edit"></i>
</a>
</td>
<td>
<a class="btn btn-danger btn-flat btn-sm" href="department_delete.php?department_id=<?php echo $row_dm['department_id']; ?>" onclick="return confirm('ຢືນຢັນການລຶບຂໍ້ມູນ!');">
<i class="nav-icon fas fa-trash"></i>
</a>
</td>
<?php } ?>
</tr>
</tbody>
</table>
ສ້າງໄຟລ໌ department_add.php ໜ້າເພີ່ມພະແນກໃໝ່
<div class="card card-info">
<div class="card-header">
<h3 class="card-title">ເພີ່ມຂໍ້ມູນພະແນກ</h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<form action="department_add_db.php" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-6">
<!-- text input -->
<div class="form-group">
<label>ຊື່ພະແນກ</label>
<input type="text" name="department_name" class="form-control" placeholder="ປ້ອນຊື່ພະແນກ">
</div>
</div>
</div>
<div class="row" align="left">
<div class="col-sm-6">
<button type="submit" class="btn btn-success">ບັນທຶກຂໍ້ມູນ</button>
<a href="department.php" class="btn btn-danger" data-dismiss="modal">ຍົກເລິກ</a>
</div>
</div>
</form>
</div>
<!-- /.card-body -->
</div>
ສ້າງໄຟລ໌ department_add_db.php
<?php
if(isset($_POST['department_name'])) {
include 'condb.php';
$department_name = $_POST['department_name'];
//check data
$stmt = $conn->prepare("SELECT department_id FROM tb_department WHERE department_name = :department_name");
$stmt->execute(array(':department_name' => $department_name));
echo '
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">';
if($stmt->rowCount() > 0){
echo '<script>
setTimeout(function() {
swal({
title: "ຂໍ້ມູນຊ້ຳ !! ",
text: "ຂໍ້ມູນຊ້ຳ!! ກະລຸນາປ້ອມຂໍ້ມູນໃໝ່",
type: "warning"
}, function() {
window.location = "department.php?act=add";
});
}, 1000);
</script>';
}else{
//sql insert
$stmt = $conn->prepare("INSERT INTO tb_department (department_name)
VALUES (:department_name)");
$stmt->bindParam(':department_name', $department_name, PDO::PARAM_STR);
$result = $stmt->execute();
if($result){
echo '<script>
setTimeout(function() {
swal({
title: "ເພີ່ມຂໍ້ມູນບໍ່ສຳເລັດ",
type: "success"
}, function() {
window.location = "department.php";
});
}, 1000);
</script>';
}else{
echo '<script>
setTimeout(function() {
swal({
title: "ເກິດຂໍ້ຜິດພາດ",
type: "error"
}, function() {
window.location = "department.php";
});
}, 1000);
</script>';
}
$conn = null; //close connect db
} //else check
} //isset
?>
ສ້າງໄຟລ໌ department_edit.php ໜ້າແກ້ໄຂພະແນກ
<?php
if(isset($_GET['department_id'])){
include 'condb.php';
$stmt = $conn->prepare("SELECT* FROM tb_department WHERE department_id=?");
$stmt->execute([$_GET['department_id']]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//ຖ້າຄິວຣີຜິດພາດໃຫ້ກັບໄປໜ້າ index
if($stmt->rowCount() < 1){
header('Location: index.php');
exit();
}
}//isset
?>
<div class="card card-info">
<div class="card-header">
<h3 class="card-title">ແກ້ໄຂຂໍ້ມູນພະແນກ</h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<form action="department_edit_db.php" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-6">
<!-- text input -->
<div class="form-group">
<label>ຊື່ພະແນກ</label>
<input type="text" name="department_name" value="<?= $row['department_name'];?>" class="form-control">
</div>
</div>
</div>
<div class="row" align="left">
<div class="col-sm-6">
<input type="hidden" name="department_id" value="<?= $row['department_id'];?>">
<button type="submit" class="btn btn-success">ບັນທຶກຂໍ້ມູນ</button>
<a href="department.php" class="btn btn-danger" data-dismiss="modal">ຍົກເລິກ</a>
</div>
</div>
</form>
</div>
<!-- /.card-body -->
</div>
ສ້າງໄຟລ໌ department_edit_db.php
<?php
if(isset($_POST['department_name'])) {
include 'condb.php';
$d_id = $_POST['department_id'];
$d_name = $_POST['department_name'];
//sql update
$stmt = $conn->prepare("UPDATE tb_department SET department_name=:department_name WHERE department_id=:department_id");
$stmt->bindParam(':department_id', $d_id , PDO::PARAM_INT);
$stmt->bindParam(':department_name', $d_name , PDO::PARAM_STR);
$stmt->execute();
// sweet alert
echo '
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">';
if($stmt->rowCount() > 0){
echo '<script>
setTimeout(function() {
swal({
title: "ແກ້ໄຂຂໍ້ມູນສຳເລັດ",
type: "success"
}, function() {
window.location = "department.php";
});
}, 1000);
</script>';
}else{
echo '<script>
setTimeout(function() {
swal({
title: "ເກິດຜິດພາດ",
type: "error"
}, function() {
window.location = "department.php";
});
}, 1000);
</script>';
}
$conn = null; //close connect db
} //isset
?>
ສ້າງໄຟລ໌ department_delete.php ລົບພະແນກ
<?php
if(isset($_GET['department_id'])){
include 'condb.php';
$d_id = $_GET['department_id'];
$stmt = $conn->prepare('DELETE FROM tb_department WHERE department_id=:department_id');
$stmt->bindParam(':department_id', $d_id , PDO::PARAM_INT);
$stmt->execute();
if($stmt->rowCount() > 0){
echo '<script>
window.location = "department.php";
</script>';
}else{
echo '<script>
window.location = "department.php";
</script>';
}
$conn = null;
} //isset
?>
ພື້ນທີ່ປະຊາສຳພັນ