-
Notifications
You must be signed in to change notification settings - Fork 1
/
insertBio.php
139 lines (119 loc) · 3.87 KB
/
insertBio.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
session_start();
include 'mylib.php'; // Include your database connection file
db_connect(); // Establish the database connection
global $db;
// Check if the user is logged in
if (!isset($_SESSION['email'])) {
header("Location: login.php");
exit();
}
$userEmail = $_SESSION['email'];
// Fetch the UserID from the session
$sql = "SELECT UserID FROM users WHERE email = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("s", $userEmail);
$stmt->execute();
$stmt->bind_result($userID);
$stmt->fetch();
$stmt->close();
if (!$userID) {
echo "User not found.";
exit();
}
// Initialize the update query
$updateQuery = "UPDATE users SET ";
$updateFields = [];
$updateParams = [];
$paramTypes = '';
// Fetch form inputs
$profilePicture = $_POST['profilePicture'] ?? '';
$bio = trim($_POST['bio'] ?? '');
$igLink = $_POST['igLink'] ?? '';
$tiktokLink = $_POST['tiktokLink'] ?? '';
$tumblrLink = $_POST['tumblrLink'] ?? '';
$twitterLink = $_POST['twitterLink'] ?? '';
$fbLink = $_POST['fbLink'] ?? '';
$personalLink = $_POST['personalLink'] ?? '';
// Update only if the field has been filled out by the user
if (!empty($bio)) {
$updateFields[] = "bio = ?";
$updateParams[] = $bio;
$paramTypes .= 's';
}
if (!empty($igLink)) {
$updateFields[] = "igLink = ?";
$updateParams[] = $igLink;
$paramTypes .= 's';
}
if (!empty($tiktokLink)) {
$updateFields[] = "tiktokLink = ?";
$updateParams[] = $tiktokLink;
$paramTypes .= 's';
}
if (!empty($tumblrLink)) {
$updateFields[] = "tumblrLink = ?";
$updateParams[] = $tumblrLink;
$paramTypes .= 's';
}
if (!empty($twitterLink)) {
$updateFields[] = "twitterLink = ?";
$updateParams[] = $twitterLink;
$paramTypes .= 's';
}
if (!empty($fbLink)) {
$updateFields[] = "fbLink = ?";
$updateParams[] = $fbLink;
$paramTypes .= 's';
}
if (!empty($personalLink)) {
$updateFields[] = "personalLink = ?";
$updateParams[] = $personalLink;
$paramTypes .= 's';
}
//if (!empty($profilePicture)) {
// $updateFields[] = "profilePicture = ?";
// $updateParams[] = $profilePicture;
// $paramTypes .= 's';
//Estella is trying to insert the profile picture here: crossing my fingers this works
// Handle profile picture upload
if (isset($_FILES['profilePicture']) && $_FILES['profilePicture']['error'] === UPLOAD_ERR_OK) {
$uploadDir = "uploads/$userID/";
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0777, true); // Create the user's directory if it doesn't exist
}
$fileTmpPath = $_FILES['profilePicture']['tmp_name'];
$fileName = basename($_FILES['profilePicture']['name']);
$destination = $uploadDir . $fileName;
// Move the uploaded file to the user's directory
if (move_uploaded_file($fileTmpPath, $destination)) {
// Update the profile picture path in the database
$updateFields[] = "profilePicture = ?";
$updateParams[] = $destination;
$paramTypes .= 's';
} else {
echo "<script>alert('Error uploading the profile picture.'); window.location.href='profile.php';</script>";
exit();
}
}
// If no fields were provided, don't perform any updates
if (empty($updateFields)) {
echo "<script>alert('No updates provided.'); window.location.href='profile.php';</script>";
exit();
}
// Finalize the query
$updateQuery .= implode(", ", $updateFields) . " WHERE UserID = ?";
$updateParams[] = $userID; // Add the user ID as the final parameter
$paramTypes .= 'i'; // 'i' for integer (UserID)
// Prepare, bind, and execute the query
$stmt = $db->prepare($updateQuery);
$stmt->bind_param($paramTypes, ...$updateParams);
$stmt->execute();
if ($stmt->affected_rows > 0) {
echo "<script>alert('Profile updated successfully!'); window.location.href='profile.php';</script>";
} else {
echo "<script>alert('No changes made to the profile.'); window.location.href='profile.php';</script>";
}
$stmt->close();
$db->close();
?>