-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_profile.php
More file actions
111 lines (95 loc) · 5 KB
/
save_profile.php
File metadata and controls
111 lines (95 loc) · 5 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
// Kết nối cơ sở dữ liệu
$link = mysqli_connect("localhost", "root", "", "dating_app") or die("Không thể kết nối cơ sở dữ liệu");
// Kiểm tra nếu form được submit
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$userID = 1; // ID người dùng hiện tại (cần thay đổi theo session)
$uploadDir = 'uploads/'; // Thư mục lưu file
// Kiểm tra nếu thư mục uploads không tồn tại thì tạo mới
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
// Nhận dữ liệu từ form
$name = mysqli_real_escape_string($link, $_POST['name']);
$dob = mysqli_real_escape_string($link, $_POST['dob']);
$location = mysqli_real_escape_string($link, $_POST['location']);
$gender = mysqli_real_escape_string($link, $_POST['gender']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$phone = mysqli_real_escape_string($link, $_POST['phone']);
$story = mysqli_real_escape_string($link, $_POST['story']);
// Kiểm tra nếu có file avatar được upload
if (isset($_FILES['avatar']) && $_FILES['avatar']['error'] === UPLOAD_ERR_OK) {
$uploadDir = 'uploads/'; // Thư mục lưu file
$fileName = uniqid() . '_' . basename($_FILES['avatar']['name']); // Tạo tên file duy nhất
$uploadFile = $uploadDir . $fileName;
// Di chuyển file vào thư mục uploads
if (move_uploaded_file($_FILES['avatar']['tmp_name'], $uploadFile)) {
$avatarURL = $uploadFile; // Lưu đường dẫn tương đối
// Lưu đường dẫn vào cơ sở dữ liệu
$sqlAvatar = "UPDATE userinformation SET Avt = '$avatarURL' WHERE ID = $userID";
mysqli_query($link, $sqlAvatar);
}
}
// Xử lý các hình ảnh khác
// Xử lý các hình ảnh khác
for ($i = 1; $i <= 6; $i++) {
$inputName = "image{$i}";
if (isset($_FILES[$inputName]) && $_FILES[$inputName]['error'] === UPLOAD_ERR_OK) {
$fileName = uniqid() . '_' . basename($_FILES[$inputName]['name']);
$uploadFile = $uploadDir . $fileName;
if (move_uploaded_file($_FILES[$inputName]['tmp_name'], $uploadFile)) {
$imageURL = $uploadFile;
// Lấy đường dẫn ảnh cũ từ cơ sở dữ liệu
$sqlGetOldImage = "SELECT imgPath FROM images WHERE UserID = $userID AND ID = $i";
$result = mysqli_query($link, $sqlGetOldImage);
if ($result && mysqli_num_rows($result) > 0) {
$oldImage = mysqli_fetch_assoc($result)['imgPath'];
// Xóa file ảnh cũ khỏi thư mục nếu tồn tại
if (file_exists($oldImage)) {
unlink($oldImage);
}
// Xóa ảnh cũ khỏi cơ sở dữ liệu
$sqlDelete = "DELETE FROM images WHERE UserID = $userID AND ID = $i";
mysqli_query($link, $sqlDelete);
}
// Lưu ảnh mới vào cơ sở dữ liệu
$sqlImage = "INSERT INTO images (UserID, imgPath, ID, IsActive) VALUES ($userID, '$imageURL', $i, 1)";
mysqli_query($link, $sqlImage);
}
}
}
// Cập nhật thông tin người dùng
$sql = "UPDATE userinformation
SET UserName = '$name', BirthDate = '$dob', UserAddress = '$location', Gender = '$gender',
Email = '$email', PhoneNumber = '$phone', bio = '$story'
WHERE ID = $userID";
mysqli_query($link, $sql);
// Xử lý sở thích
if (isset($_POST['hobby'])) {
$hobbies = $_POST['hobby'];
mysqli_query($link, "DELETE FROM userhobbby WHERE UserID = $userID");
foreach ($hobbies as $hobby) {
$hobbyID = mysqli_fetch_assoc(mysqli_query($link, "SELECT ID FROM hobbylist WHERE HobbyName = '$hobby'"))['ID'];
mysqli_query($link, "INSERT INTO userhobbby (UserID, HobbyID) VALUES ($userID, $hobbyID)");
}
}
// Xử lý tính cách
if (isset($_POST['personally'])) {
$personally = $_POST['personally'];
mysqli_query($link, "DELETE FROM userpersonally WHERE UserID = $userID");
foreach ($personally as $trait) {
$personallyID = mysqli_fetch_assoc(mysqli_query($link, "SELECT ID FROM personallylist WHERE PersonallyName = '$trait'"))['ID'];
mysqli_query($link, "INSERT INTO userpersonally (UserID, PersonallyID) VALUES ($userID, $personallyID)");
}
}
// Xử lý trạng thái "Looking For"
if (isset($_POST['looking-for'])) {
$lookingFor = mysqli_real_escape_string($link, $_POST['looking-for']);
$lookingID = mysqli_fetch_assoc(mysqli_query($link, "SELECT ID FROM looking WHERE LookingName = '$lookingFor'"))['ID'];
mysqli_query($link, "DELETE FROM userlooking WHERE UserID = $userID");
mysqli_query($link, "INSERT INTO userlooking (UserID, LookingID) VALUES ($userID, $lookingID)");
}
// Chuyển hướng sau khi lưu
header("Location: profileuser.php");
exit();
}