1. Tạo model kết nối database
-- Tạo file: model/connection.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php //server info $serverName = "localhost"; $serverUser = "root"; $serverPassword = ""; $database = "phongkhamdemo"; //connect database $conn = new mysqli($serverName,$serverUser,$serverPassword,$database); //check connection if($conn->connect_error){ die("Connection failed"); } ?> |
2. Hiển thị danh sách User
-- Tạo file: view/admin/user_list.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<!DOCTYPE html> <html> <head> <title>User Management</title> </head> <body> <table border="1" style="border-collapse: collapse;"> <thead> <tr> <th>ID</th> <th>Username</th> <th>Password</th> <th>Email</th> <th>Phone</th> <th>Address</th> <th>Permission</th> <th>Action</th> </tr> </thead> <tbody> <?php require_once("../../model/connection.php"); $sql = "SELECT * FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($user = $result->fetch_assoc()) { ?> <tr> <th><?php echo $user["userID"] ?></th> <td><?php echo $user["username"] ?></td> <td><?php echo $user["password"] ?></td> <td><?php echo $user["email"] ?></td> <td><?php echo $user["phone"] ?></td> <td><?php echo $user["address"] ?></td> <td><?php echo $user["permission"] ?></td> <td> <a href="user_edit.php?id=<?php echo $user["userID"] ?>">Sửa</a> <a href="user_delete.php?id=<?php echo $user["userID"] ?>">Xóa</a> </td> </tr> <?php } } ?> </tbody> </table> <a href="user_add.php"><button>Add User</button></a> </body> </html> |
3. Thêm User
-- Tạo file: view/admin/user_add.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<!DOCTYPE html> <html> <head> <title>Add User</title> </head> <body> <?php require_once("../../model/connection.php"); if (isset($_POST["save"])) { $username = $_POST["username"]; $password = $_POST["password"]; $email = $_POST["email"]; $phone = $_POST["phone"]; $address = $_POST["address"]; $permission = $_POST["permission"]; $sql = "INSERT INTO users (username,password,email,phone,address,permission) VALUES ('$username','$password','$email','$phone','$address','$permission')"; if ($conn->query($sql) === TRUE) { header('Location: user_list.php'); } else { echo "Error updating record: " . $conn->error; } } $conn->close(); ?> <form method="POST"> <label for="username">Username:</label><br> <input type="text" name="username"><br> <label for="password">Password:</label><br> <input type="password" name="password"><br> <label for="email">Email:</label><br> <input type="text" name="email"><br> <label for="phone">Phone:</label><br> <input type="text" name="phone"><br> <label for="address">Address:</label><br> <input type="text" name="address"><br> <label for="permission">Permission:</label><br> <select name="permission" required> <option selected disabled hidden>Please select permission</option> <option value="1">Administrator</option> <option value="2">Free User</option> <option value="3">Basic User</option> <option value="4">VIP User</option> </select><br> <input type="submit" name="save" value="Submit" ><br> </form> </body> </html> |
4. Sửa User
-- Tạo file: view/admin/user_edit.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
<!DOCTYPE html> <html> <head> <title>Edit User</title> </head> <body> <?php require_once("../../model/connection.php"); $id = ""; $username = ""; $password = ""; $email = ""; $phone = ""; $address = ""; $permission = ""; if (isset($_GET["id"])) { $id = $_GET["id"]; $sql = "SELECT * FROM users WHERE userID = $id"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($user = $result->fetch_assoc()) { $id = $user["userID"]; $username = $user["username"]; $password = $user["password"]; $email = $user["email"]; $phone = $user["phone"]; $address = $user["address"]; $permission = $user["permission"]; } } } if (isset($_POST["save"])) { $username = $_POST["username"]; $password = $_POST["password"]; $email = $_POST["email"]; $phone = $_POST["phone"]; $address = $_POST["address"]; $permission = $_POST["permission"]; $sql = "UPDATE users SET username='$username',password='$password',email='$email', phone='$phone',address='$address',permission='$permission' WHERE userID='$id'"; if ($conn->query($sql) === TRUE) { header('Location: user_list.php'); } else { echo "Error updating record: " . $conn->error; } } $conn->close(); ?> <form method="POST"> <label for="username">Username:</label><br> <input type="text" name="username" value="<?php echo $username; ?>"><br> <label for="password">Password:</label><br> <input type="password" name="password" value="<?php echo $password; ?>"><br> <label for="email">Email:</label><br> <input type="text" name="email" value="<?php echo $email; ?>"><br> <label for="phone">Phone:</label><br> <input type="text" name="phone" value="<?php echo $phone; ?>"><br> <label for="address">Address:</label><br> <input type="text" name="address" value="<?php echo $address; ?>"><br> <label for="permission">Permission:</label><br> <select name="permission" required> <option selected disabled hidden>Please select permission</option> <option value="1" <?php echo ($permission == 1) ? "selected" : ""; ?>>Administrator</option> <option value="2" <?php echo ($permission == 2) ? "selected" : ""; ?>>Free User</option> <option value="3" <?php echo ($permission == 3) ? "selected" : ""; ?>>Basic User</option> <option value="4" <?php echo ($permission == 4) ? "selected" : ""; ?>>VIP User</option> </select><br> <input type="submit" value="Submit" name="save"><br> </form> </body> </html> |
5. Xóa User
-- Tạo file: view/admin/user_delete.php
1 2 3 4 5 6 7 8 9 10 11 |
<?php require_once("../../model/connection.php"); if (isset($_GET["id"])) { $id = $_GET["id"]; $sql = "DELETE FROM users WHERE userID = $id"; if ($conn->query($sql) === TRUE) { header('Location: user_list.php'); } else { echo "Error: " . $conn->error; } } |