Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions database/connect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
$server = 'localhost:3306';
$user = 'root';
$pass = '';
$database = 'web_pka';

$connect = new mysqli(
$server,
$user,
$pass,
$database
);

if ($connect) {
mysqli_query($connect, "SET NAMES 'utf8' ");
echo "success";
} else {
echo "error";
}

?>
55 changes: 55 additions & 0 deletions database/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<h1>Danh mục sinh viên đăng ký môn học</h1>
<style>
table {
width: 100%;
border-collapse: collapse;
}

table,
th,
td {
border: 1px solid black;
padding: 8px;
}
</style>
<table style="border: 1;">
<tr>
<th>MSSV</th>
<th>Họ tên</th>
<th>Kỳ</th>
<th>Đăng ký</th>
</tr>
<?php
$server = 'localhost:3306';
$user = 'root';
$pass = '';
$database = 'web_pka';
$conn = new mysqli($server, $user, $pass, $database);
// Kiểm tra kết nối
if ($conn->connect_error) {
die("Kết nối CSDL thất bại: " . $conn->connect_error);
}
$sql = "SELECT sinhvien.MSSV, sinhvien.HoTen, monhoc.MaMH, monhoc.TenMH, dangky.Ky
FROM dangky
JOIN sinhvien ON dangky.MSSV = sinhvien.MSSV
JOIN monhoc ON dangky.MaMH = monhoc.MaMH;";
$result = $conn->query($sql);
if ($result) {
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["MSSV"] . "</td>";
echo "<td>" . $row["HoTen"] . "</td>";
echo "<td>" . $row["Ky"] . "</td>";
echo "<td>" . $row["TenMH"] . "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='5'>Không có dữ liệu</td></tr>";
}
} else {
echo "Error: " . $conn->error;
}
$conn->close();
?>
</table>
34 changes: 34 additions & 0 deletions session-login/create-file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
session_start();

if (!isset($_SESSION["username"])) {
header("Location: login.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create File</title>
<!-- Bootstrap CSS -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<form action="upload-file.php" method="post" enctype="multipart/form-data" class="mt-3">
<div class="form-group">
<label for="file">Chọn tập tin để tải lên:</label>
<input type="file" class="form-control-file" id="file" name="file">
</div>
<button type="submit" class="btn btn-primary">Tải lên</button>
</form>
</div>

<!-- Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
42 changes: 42 additions & 0 deletions session-login/delete-file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "web_pka";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
die("Kết nối đến cơ sở dữ liệu thất bại: " . $conn->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["id"])) {
$id = $_POST["id"];

$sql = "SELECT * FROM files WHERE id = $id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$fileName = $row["name"];

$filePath = "uploads/" . $fileName;
if (file_exists($filePath)) {
unlink($filePath);
}

$sql = "DELETE FROM files WHERE id = $id";
if ($conn->query($sql) === TRUE) {
echo "Tệp $fileName đã được xóa thành công.";
} else {
echo "Lỗi khi xóa tệp: " . $conn->error;
}
} else {
echo "Không tìm thấy tệp cần xóa.";
}
} else {
echo "Yêu cầu không hợp lệ.";
}

$conn->close();
?>
39 changes: 39 additions & 0 deletions session-login/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
session_start();

if (!isset($_SESSION["username"])) {
header("Location: login.php");
exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Trang chủ</title>
<!-- Bootstrap CSS -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h1>Chào mừng <?php echo $_SESSION["username"]; ?> đến với trang index!</h1>
<div class="card mt-3">
<div class="card-body">
<h5 class="card-title">Thông tin của bạn:</h5>
<p class="card-text">Tên đăng nhập: <?php echo $_SESSION["username"]; ?></p>
<form class="d-flex" action="logout.php" method="post">
<button type="submit" class="btn btn-primary">Logout</button>
<a class="btn btn-info" style="margin-left: 10px" href="upload.php">Upload file</a>
</form>
</div>
</div>
</div>

<!-- Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
39 changes: 39 additions & 0 deletions session-login/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row justify-content-center mt-5">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h2 class="text-center">Login</h2>
</div>
<div class="card-body">
<form action="validateuser.php" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions session-login/logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
session_start();
session_unset();
session_destroy();
header("Location: login.php");
exit;
?>
59 changes: 59 additions & 0 deletions session-login/sql/web_pka.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Navicat Premium Data Transfer

Source Server : localhost_3306
Source Server Type : MySQL
Source Server Version : 80030 (8.0.30)
Source Host : localhost:3306
Source Schema : web_pka

Target Server Type : MySQL
Target Server Version : 80030 (8.0.30)
File Encoding : 65001

Date: 05/05/2024 13:43:56
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for files
-- ----------------------------
DROP TABLE IF EXISTS `files`;
CREATE TABLE `files` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`type` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`size` int NOT NULL,
`upload_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of files
-- ----------------------------
INSERT INTO `files` VALUES (2, 'ĐẠT ĐK TTTN.pdf', 'application/pdf', 265370, '2024-05-05 06:27:25');
INSERT INTO `files` VALUES (4, '20240505063803.sql', 'application/octet-stream', 616349, '2024-05-05 06:38:03');
INSERT INTO `files` VALUES (5, '20240505064252.pdf', 'application/pdf', 8543122, '2024-05-05 06:42:52');

-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int UNSIGNED NOT NULL AUTO_INCREMENT,
`username` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`password` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`level` int NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES (1, 'hungdm', '$2y$10$9k6oY8LTFLXd/Vp7b18wbejsciXof..kCSEr21Bvk7iabOZHddjOy', 1, '2024-05-05 12:55:05', '2024-05-05 12:55:10');

SET FOREIGN_KEY_CHECKS = 1;
53 changes: 53 additions & 0 deletions session-login/upload-file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

session_start();

if (!isset($_SESSION["username"])) {
header("Location: login.php");
exit;
}
$servername = "localhost";
$username = "root";
$password = "";
$database = "web_pka";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
die("Kết nối đến cơ sở dữ liệu thất bại: " . $conn->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["file"])) {
$targetDir = "uploads/";
if (!file_exists($targetDir)) {
mkdir($targetDir, 0777, true);
}

$targetFile = $targetDir . basename($_FILES["file"]["name"]);

$fileName = $_FILES["file"]["name"];
$fileType = $_FILES["file"]["type"];
$fileSize = $_FILES["file"]["size"];
$fileTmpName = $_FILES["file"]["tmp_name"];
$fileUploadDate = date("Y-m-d H:i:s");

$currentDateTime = date("YmdHis");
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
$newFileName = $currentDateTime . "." . $fileExtension;

$sql = "INSERT INTO files (name, type, size, upload_date) VALUES ('$newFileName', '$fileType', '$fileSize', '$fileUploadDate')";
if ($conn->query($sql) === FALSE) {
echo "Lỗi khi lưu thông tin tệp vào cơ sở dữ liệu: " . $conn->error;
}

if (move_uploaded_file($fileTmpName, $targetFile)) {
header("Location: upload.php");
} else {
echo "Có lỗi xảy ra khi tải lên tệp.";
}
} else {
echo "Vui lòng chọn một tệp để tải lên.";
}

$conn->close();
?>
Loading