Skip to content
44 changes: 44 additions & 0 deletions BT/BT1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dữ liệu sinh viên đăng ký môn học</title>
</head>
<body>
<h1>Dữ liệu sinh viên đăng ký môn học</h1>
<?php
// Kết nối đến cơ sở dữ liệu
$servername = "localhost"; // Tên máy chủ MySQL
$username = "username"; // Tên người dùng MySQL
$password = "password"; // Mật khẩu MySQL
$dbname = "PKA_S"; // Tên cơ sở dữ liệu MySQL

// Tạo kết nối
$conn = new mysqli($servername, $username, $password, $dbname);

// Kiểm tra kết nối
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Truy vấn dữ liệu từ bảng SinhVien và DangKy
$sql = "SELECT SinhVien.MSSV, SinhVien.HoTen, MonHoc.MaMH, MonHoc.TenMH, DangKy.Ky
FROM SinhVien
INNER JOIN DangKy ON SinhVien.MSSV = DangKy.MSSV
INNER JOIN MonHoc ON DangKy.MaMH = MonHoc.MaMH";
$result = $conn->query($sql);

// Kiểm tra và hiển thị dữ liệu
if ($result->num_rows > 0) {
// Duyệt qua từng hàng kết quả
while($row = $result->fetch_assoc()) {
echo "MSSV: " . $row["MSSV"]. " - Họ và tên: " . $row["HoTen"]. " - Mã môn học: " . $row["MaMH"]. " - Tên môn học: " . $row["TenMH"]. " - Kỳ: " . $row["Ky"]. "<br>";
}
} else {
echo "Không có dữ liệu";
}
$conn->close();
?>
</body>
</html>
18 changes: 18 additions & 0 deletions BT/BT1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE SinhVien (
MSSV VARCHAR(10) PRIMARY KEY,
HoTen VARCHAR(255)
);

CREATE TABLE MonHoc (
MaMH VARCHAR(10) PRIMARY KEY,
TenMH VARCHAR(255)
);

CREATE TABLE DangKy (
MSSV VARCHAR(10),
MaMH VARCHAR(10),
Ky VARCHAR(10),
FOREIGN KEY (MSSV) REFERENCES SinhVien(MSSV),
FOREIGN KEY (MaMH) REFERENCES MonHoc(MaMH),
PRIMARY KEY (MSSV, MaMH, Ky)
);
8 changes: 8 additions & 0 deletions BT/Tuan 3/Bai 2/BT2-W3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SQL
CREATE TABLE files (
id INT PRIMARY KEY AUTO_INCREMENT,
filename VARCHAR(255) NOT NULL,
filetype VARCHAR(50) NOT NULL,
upload_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
filesize BIGINT NOT NULL
);
13 changes: 13 additions & 0 deletions BT/Tuan 3/Bai 2/db_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
$host = 'localhost:3307'; // Hoặc địa chỉ IP của server MySQL
$dbname = 'bt2-w3';
$username = 'root';
$password = '';

try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Could not connect to the database $dbname :" . $e->getMessage());
}
?>
83 changes: 83 additions & 0 deletions BT/Tuan 3/Bai 2/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
session_start();

// Kết nối CSDL (sử dụng file connect.php)
require_once('includes/db_config.php');

// Xử lý form tải lên tệp tin
if (isset($_POST['upload'])) {
// Lấy dữ liệu từ form
$target_dir = "upload/"; // Thư mục lưu trữ tệp tin
$file_name = basename($_FILES['myfile']['name']); // Tên tệp tin
$file_type = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); // Loại tệp tin
$temp_file = $_FILES['myfile']['tmp_name']; // Vị trí tệp tin tạm thời

// Kiểm tra kích thước tệp tin
if ($_FILES['myfile']['size'] > 2097152) { // 2MB
$error = "Kích thước tệp tin vượt quá giới hạn (2MB).";
}

// Kiểm tra loại tệp tin hợp lệ
$allow_types = array('jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx', 'xls', 'xlsx');
if (!in_array($file_type, $allow_types)) {
$error = "Chỉ hỗ trợ các loại tệp tin: " . implode(', ', $allow_types) . ".";
}

// Nếu không có lỗi, tiến hành tải lên tệp tin
if (!$error) {
// Tạo tên tệp tin mới theo định dạng: YYYYMMDD_HHMMSS_Mã_ngẫu_nhiên.ext
$new_file_name = date('YmdHis') . '_' . uniqid() . '.' . $file_type;
$target_file = $target_dir . $new_file_name;

// Di chuyển tệp tin từ vị trí tạm thời vào thư mục đích
if (move_uploaded_file($temp_file, $target_file)) {
// Lưu thông tin tệp tin vào CSDL
$sql = "INSERT INTO files (name, type, size, uploaded_at) VALUES ('$new_file_name', '$file_type', '" . $_FILES['myfile']['size'] . "', NOW())";
mysqli_query($conn, $sql);

if (mysqli_affected_rows($conn) > 0) {
$success = "Tải lên tệp tin thành công!";
} else {
$error = "Lỗi khi lưu thông tin tệp tin.";
}
} else {
$error = "Lỗi khi di chuyển tệp tin.";
}
}
}

// Lấy danh sách tệp tin từ CSDL
$sql = "SELECT * FROM files ORDER BY name ASC"; // Sắp xếp theo tên tệp tin (tăng dần)
if (isset($_GET['sort']) && $_GET['sort'] == 'date') {
$sql = "SELECT * FROM files ORDER BY uploaded_at DESC"; // Sắp xếp theo ngày tải lên (giảm dần)
}
$result = mysqli_query($conn, $sql);

?>

<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quản lý tệp tin</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>Quản lý tệp tin</h1>

<?php if (isset($error)) echo "<p class='error'>$error</p>"; ?>
<?php if (isset($success)) echo "<p class='success'>$success</p>"; ?>

<h2>Tải lên tệp tin</h2>
<form action="index.php" method="post" enctype="multipart/form-data">
<label for="myfile">Chọn tệp tin:</label>
<input type="file" id="myfile" name="myfile" required>
<button type="submit" name="upload">Tải lên</button>
</form>

<h2>Danh sách tệp tin</h2>
<table>
<thead>
<tr>
<th>Tên tệp tin
72 changes: 72 additions & 0 deletions BT/Tuan 3/Bai 2/register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
session_start();

// Kết nối CSDL (sử dụng file connect.php)
require_once('includes/db_config.php');

// Xử lý form đăng ký
if (isset($_POST['register'])) {
// Lấy dữ liệu từ form
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$email = trim($_POST['email']);

// Kiểm tra dữ liệu đầu vào
if (empty($username) || empty($password) || empty($email)) {
$error = "Vui lòng nhập đầy đủ thông tin.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Địa chỉ email không hợp lệ.";
} else {
// Kiểm tra username đã tồn tại hay chưa
$sql = "SELECT * FROM users WHERE username = '$username'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
$error = "Tên đăng nhập đã tồn tại.";
} else {
// Mã hóa mật khẩu bằng SHA1
$hashed_password = sha1($password);

// Lưu thông tin người dùng vào CSDL
$sql = "INSERT INTO users (username, password, email) VALUES ('$username', '$hashed_password', '$email')";
mysqli_query($conn, $sql);

if (mysqli_affected_rows($conn) > 0) {
$success = "Đăng ký thành công!";
} else {
$error = "Lỗi khi đăng ký.";
}
}
}
}

?>

<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Đăng ký</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>Đăng ký</h1>

<?php if (isset($error)) echo "<p class='error'>$error</p>"; ?>
<?php if (isset($success)) echo "<p class='success'>$success</p>"; ?>

<form action="register.php" method="post">
<label for="username">Tên đăng nhập:</label>
<input type="text" id="username" name="username" required>

<label for="password">Mật khẩu:</label>
<input type="password" id="password" name="password" required>

<label for="email">Email:</label>
<input type="email" id="email" name="email" required>

<button type="submit" name="register">Đăng ký</button>
</form>
</body>
</html>
53 changes: 53 additions & 0 deletions BT/Tuan 3/Bai 2/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}

h1 {
text-align: center;
margin-bottom: 20px;
}

form {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
}

form label {
display: block;
margin-bottom: 5px;
}

form input[type="file"] {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
box-sizing: border-box;
}

form button {
padding: 8px 15px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}

table {
width: 100%;
margin-top: 20px;
border-collapse: collapse;
}

th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}

th {
background-color: #f2f2f2;
}
87 changes: 87 additions & 0 deletions BT/Tuan 3/Bai 2/upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
include 'db_config.php';

// Xử lý tải tệp lên
$message = '';
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["fileToUpload"])) {
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$uploadOk = 1;

// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
$message = "Sorry, your file is too large.";
$uploadOk = 0;
}

// Allow certain file formats
if ($fileType != "jpg" && $fileType != "png" && $fileType != "jpeg" && $fileType != "gif") {
$message = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}

if ($uploadOk == 0) {
$message = "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$sql = "INSERT INTO files (name, type, size) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->execute([
basename($_FILES["fileToUpload"]["name"]),
$fileType,
$_FILES["fileToUpload"]["size"]
]);
$message = "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been uploaded.";
} else {
$message = "Sorry, there was an error uploading your file.";
}
}
}

// Hiển thị danh sách các tệp đã tải lên
$sql = "SELECT * FROM files ORDER BY uploaded_at DESC";
$files = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Manager</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>File Upload & Display</h1>
<div>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>
</div>
<?php if ($message) echo "<p>$message</p>"; ?>

<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Size</th>
<th>Date Uploaded</th>
</tr>
</thead>
<tbody>
<?php while ($file = $files->fetch()) : ?>
<tr>
<td><?php echo htmlspecialchars($file['name']); ?></td>
<td><?php echo htmlspecialchars($file['type']); ?></td>
<td><?php echo number_format($file['size'] / 1024, 2) . ' KB'; ?></td>
<td><?php echo htmlspecialchars($file['uploaded_at']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added BT/Tuan 3/Bai 3/BT3-W3.sql
Empty file.
Empty file added BT/Tuan 3/Bai 3/db_config.php
Empty file.
Empty file added BT/Tuan 3/Bai 3/index.php
Empty file.
Loading