Skip to content

Instantly share code, notes, and snippets.

@thuan1412
Created October 8, 2021 03:05
Show Gist options
  • Save thuan1412/e418312167430c7c43712112acce9626 to your computer and use it in GitHub Desktop.
Save thuan1412/e418312167430c7c43712112acce9626 to your computer and use it in GitHub Desktop.
<?php
global $conn;
// Hàm kết nối database
function connect_db()
{
// Gọi tới biến toàn cục $conn
global $conn;
// Nếu chưa kết nối thì thực hiện kết nối
if (!$conn){
$conn = mysqli_connect('localhost', 'root', '', 'qlcongty') or die ('Cannot not connect to database');
// Thiết lập font chữ kết nối
mysqli_set_charset($conn, 'utf8');
}
}
// Hàm ngắt kết nối
function disconnect_db()
{
// Gọi tới biến toàn cục $conn
global $conn;
// Nếu đã kêt nối thì thực hiện ngắt kết nối
if ($conn){
mysqli_close($conn);
}
}
// Hàm lấy tất cả sinh viên
function get_all_congtys()
{
// Gọi tới biến toàn cục $conn
global $conn;
// Hàm kết nối
connect_db();
// Câu truy vấn lấy tất cả congty
$sql = "select * from tbl_dscongty";
// Thực hiện câu truy vấn
$query = mysqli_query($conn, $sql);
// Mảng chứa kết quả
$result = array();
// Lặp qua từng record và đưa vào biến kết quả
if ($query){
while ($row = mysqli_fetch_assoc($query)){
$result[] = $row;
}
}
// Trả kết quả về
return $result;
}
// Hàm lấy sinh viên theo ID
function get_congty($maCongTy)
{
global $conn;
// Hàm kết nối
connect_db();
// Câu truy vấn lấy tất cả sinh viên
$sql = "select * from tbl_dscongty where macty = '{$maCongTy}'";
// Thực hiện câu truy vấn
$query = mysqli_query($conn, $sql);
// Trả kết quả về
return mysqli_fetch_assoc($query);
}
// Hàm thêm sinh viên
function add_congty($maCongTy, $diaChi, $ten, $dienThoai, $email, $date, $giamdoc, $linhVuc)
{
// Gọi tới biến toàn cục $conn
global $conn;
// Hàm kết nối
connect_db();
// Chống SQL Injection
$maCongTy = addslashes($maCongTy);
$diaChi = addslashes($diaChi);
$ten = addslashes($ten);
$dienThoai = addslashes($dienThoai);
$email = addslashes($email);
$date = addslashes($date);
$giamdoc = addslashes($giamdoc);
$linhVuc = addslashes($linhVuc);
// Câu truy vấn thêm
$sql = "
INSERT INTO tbl_hoso(macty, tencty, diachi, dthoai, email, namtlap, giamdoc, linhvuc) VALUES
('$maCongTy', '$ten', '$diaChi', '$dienThoai', '$email', '$date', '$giamdoc', '$linhVuc')
";
// Thực hiện câu truy vấn
$query = mysqli_query($conn, $sql);
return $query;
}
// Hàm sửa sinh viên
function edit_student($SoHoSo, $HoDem, $Ten, $Ngaysinh, $Gioitinh, $Diachi, $Truong, $MaNganh, $KhoiThi)
{
// Gọi tới biến toàn cục $conn
global $conn;
// Hàm kết nối
connect_db();
// Chống SQL Injection
$SoHoSo = addslashes($SoHoSo);
$HoDem = addslashes($HoDem);
$Ten = addslashes($Ten);
$Ngaysinh = addslashes($Ngaysinh);
$Gioitinh = addslashes($Gioitinh);
$Diachi = addslashes($Diachi);
$Truong = addslashes($Truong);
$MaNganh = addslashes($MaNganh);
$KhoiThi = addslashes($KhoiThi);
// Câu truy sửa
$sql = "
UPDATE tbl_hoso SET
SoHoSo = '$SoHoSo',
HoDem = '$HoDem',
Ten = '$Ten',
Ngaysinh = '$Ngaysinh',
Gioitinh = '$Gioitinh',
Diachi = '$Diachi',
Truong = '$Truong',
MaNganh = '$MaNganh',
KhoiThi = '$KhoiThi'
WHERE SoHoSo like '$SoHoSo'
";
// Thực hiện câu truy vấn
$query = mysqli_query($conn, $sql);
return $query;
}
// Hàm xóa sinh viên
function delete_student($SoHoSo)
{
echo $SoHoSo;
// Gọi tới biến toàn cục $conn
global $conn;
// Hàm kết nối
connect_db();
// Câu truy sửa
$sql = "
DELETE FROM tbl_hoso
WHERE SoHoSo Like '$SoHoSo'
";
// Thực hiện câu truy vấn
$query = mysqli_query($conn, $sql);
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment