-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_profile.php
156 lines (135 loc) · 5.11 KB
/
edit_profile.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
session_start();
require "includes/database_connect.php";
if (!isset($_SESSION["user_id"])) {
header("location: index.php");
die();
}
$user_id = $_SESSION['user_id'];
$sql = "SELECT * FROM users WHERE id = $user_id";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);
if (!$user) {
echo "Something went wrong!";
return;
}
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$newFullName = $_POST["full_name"];
$newEmail = $_POST["email"];
$newPhone = $_POST["phone"];
$newCollegeName = $_POST["college_name"];
// Handle image upload
if (isset($_FILES["profile_picture"]) && $_FILES["profile_picture"]["error"] === UPLOAD_ERR_OK) {
$uploadDir = 'img/'; // Define your upload directory
$newFileName = uniqid() . "_" . $_FILES["profile_picture"]["name"];
$uploadPath = $uploadDir . $newFileName;
if (move_uploaded_file($_FILES["profile_picture"]["tmp_name"], $uploadPath)) {
$updateImageSql = "UPDATE users SET profile_picture = '$uploadPath' WHERE id = $user_id";
$updateImageResult = mysqli_query($conn, $updateImageSql);
if (!$updateImageResult) {
$errorMessage = "Image update failed. Please try again.";
}
} else {
$errorMessage = "Image upload failed. Please try again.";
}
}
$updateSql = "UPDATE users SET full_name = '$newFullName', email = '$newEmail', phone = '$newPhone', college_name = '$newCollegeName' WHERE id = $user_id";
$updateResult = mysqli_query($conn, $updateSql);
if ($updateResult) {
header("location: dashboard.php");
die();
} else {
$errorMessage = "Update failed. Please try again.";
}
}
?>
<!-- HTML form for editing profile -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Edit Profile | PG Life</title>
<!-- Font Awesome 4.5 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="css/common.css">
<style>
/* Style for the edit profile form container */
.edit-profile-form {
max-width: 500px;
margin: 20px auto;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
background-color: #fff;
}
/* Style for form labels */
.edit-profile-form label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
/* Style for form input fields */
.edit-profile-form input {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
/* Style for the submit button */
.edit-profile-form button {
display: block;
width: 100%;
padding: 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
}
/* Style for the submit button on hover */
.edit-profile-form button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<?php include "includes/header.php"; ?>
<?php include "includes/head_links.php"; ?>
<nav aria-label="breadcrumb">
<ol class="breadcrumb py-2">
<li class="breadcrumb-item">
<a href="index.php">Home</a>
</li>
<li class="breadcrumb-item">
<a href="dashboard.php">Dashboard</a>
</li>
<li class="breadcrumb-item active" aria-current="page">
Edit Profile
</li>
</ol>
</nav>
<div class="edit-profile-form">
<h1>Edit Profile</h1>
<?php if (isset($errorMessage)) {
echo "<p>$errorMessage</p>";
} ?>
<form method="POST" enctype="multipart/form-data">
<label for="full_name">Full Name:</label>
<input type="text" id="full_name" name="full_name" value="<?= $user['full_name'] ?>" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="<?= $user['email'] ?>" required>
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone" value="<?= $user['phone'] ?>" required>
<label for="college_name">College Name:</label>
<input type="text" id="college_name" name="college_name" value="<?= $user['college_name'] ?>" required>
<label for="profile_picture">Profile Picture:</label>
<input type="file" id="profile_picture" name="profile_picture">
<button type="submit">Save Changes</button>
</form>
</div>
<?php include "includes/footer.php"; ?>
</body>
</html>