0% found this document useful (0 votes)
27 views6 pages

PHP Ontap

Tài liệu chứa mã PHP cho một ứng dụng quản lý tài khoản với các chức năng kết nối cơ sở dữ liệu, thêm, sửa và xóa tài khoản từ bảng 'tblontap'. Nó bao gồm các tệp 'ketnoi.php' để kết nối MySQL, 'xoa.php' để xóa tài khoản, 'index.php' để hiển thị danh sách tài khoản, 'them.php' để thêm tài khoản mới và 'sua.php' để chỉnh sửa tài khoản. Mỗi tệp đều xử lý các yêu cầu HTTP và hiển thị giao diện người dùng tương ứng.

Uploaded by

linhduc4122004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

PHP Ontap

Tài liệu chứa mã PHP cho một ứng dụng quản lý tài khoản với các chức năng kết nối cơ sở dữ liệu, thêm, sửa và xóa tài khoản từ bảng 'tblontap'. Nó bao gồm các tệp 'ketnoi.php' để kết nối MySQL, 'xoa.php' để xóa tài khoản, 'index.php' để hiển thị danh sách tài khoản, 'them.php' để thêm tài khoản mới và 'sua.php' để chỉnh sửa tài khoản. Mỗi tệp đều xử lý các yêu cầu HTTP và hiển thị giao diện người dùng tương ứng.

Uploaded by

linhduc4122004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Ketnoi.

php
<?php
// ketnoi.php: Kết nối cơ sở dữ liệu MySQL
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ontap";// Thay đổi "ten_cua_ban" thành tên database của b
// 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("Kết nối thất bại: " . $conn->connect_error);
}
?>

xoa.php
<!-- xoa.php: Xóa dữ liệu -->

<?php

include 'ketnoi.php';

$id = $_GET['id'];

$sql = "DELETE FROM tblontap WHERE id=$id";

if ($conn->query($sql) === TRUE) {

header('Location: index.php');

} else {

echo "Lỗi: " . $sql . "<br>" . $conn->error;

?>
Index.php
<?php
include 'ketnoi.php';
$sql = "SELECT * FROM tblontap";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Danh sách tài khoản</title>
</head>
<body>
<h1>Danh sách tài khoản</h1>
<a href="them.php">Thêm tài khoản</a>
<table>
<thead>
<tr>
<th>ID</th>
<th>Tên tài khoản</th>
<th>Mật khẩu</th>
<th>Hành động</th>
</tr>
</thead>
<tbody>
<?php if ($result->num_rows > 0): ?>
<?php while($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['tentk']; ?></td>
<td><?php echo $row['matkhau']; ?></td>
<td>
<a href="sua.php?id=<?php echo $row['id']; ?>">Sửa</a> |
<a href="xoa.php?id=<?php echo $row['id']; ?>" onclick="return
confirm('Bạn có chắc chắn muốn xóa?');">Xóa</a>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="4">Không có dữ liệu</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</body>
</html>
<?php
$conn->close(); // Đóng kết nối
?>
Them.php
<!-- them.php: Thêm dữ liệu -->
<?php
include 'ketnoi.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_POST['id'];
$tentk = $_POST['tentk'];
$matkhau = $_POST['matkhau'];
$sql = "INSERT INTO tblontap (id,tentk, matkhau) VALUES ('$id','$tentk', '$matkhau')";
if ($conn->query($sql) === TRUE) {
header('Location: index.php');
} else {
echo "Lỗi: " . $sql . "<br>" . $conn->error;
}
}
?>
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Thêm tài khoản</title>
</head>
<body>
<h1>Thêm tài khoản</h1>
<form method="post" action="">
<label for="id">id:</label><br>
<input type="text" id="id" name="id" required><br>
<label for="tentk">Tên tài khoản:</label><br>
<input type="text" id="tentk" name="tentk" required><br>
<label for="matkhau">Mật khẩu:</label><br>
<input type="password" id="matkhau" name="matkhau" required><br><br>
<button type="submit">Thêm</button>
</form>
</body>
</html>

sua.php
<!-- sua.php: Sửa dữ liệu -->
<?php
include 'ketnoi.php';
$id = $_GET['id'];
$sql = "SELECT * FROM tblontap WHERE id=$id";
$result = $conn->query($sql);
$row = $result->fetch_assoc();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {


$id = $_POST['id'];
$tentk = $_POST['tentk'];
$matkhau = $_POST['matkhau'];

$sql = "UPDATE tblontap SET tentk='$tentk', matkhau='$matkhau' WHERE id=$id";


if ($conn->query($sql) === TRUE) {
header('Location: index.php');
} else {
echo "Lỗi: " . $sql . "<br>" . $conn->error;
}
}
?>
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sửa tài khoản</title>
</head>
<body>
<h1>Sửa tài khoản</h1>
<form method="post" action="">
<label for="id">id:</label><br>
<input type="text" id="id" name="id" value="<?php echo $row['id']; ?>"
required><br>
<label for="tentk">Tên tài khoản:</label><br>
<input type="text" id="tentk" name="tentk" value="<?php echo $row['tentk']; ?>"
required><br>
<label for="matkhau">Mật khẩu:</label><br>
<input type="password" id="matkhau" name="matkhau" value="<?php echo
$row['matkhau']; ?>" required><br><br>
<button type="submit">Cập nhật</button>
</form>
</body>
</html>

You might also like