-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-profile.php
224 lines (187 loc) · 4.96 KB
/
update-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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
// Assuming you have established a database connection
// Replace the database credentials with your own
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "bank_db";
// Create a new connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get form data
$username = $_POST["username"];
$email = $_POST["email"];
$bio = $_POST["bio"];
// Start building the SQL query
$sql = "UPDATE users SET ";
// Initialize an array to store the values for binding
$values = array();
// Check if the username field is filled
if (!empty($username)) {
$sql .= "username=?, ";
$values[] = $username;
}
// Check if the email field is filled
if (!empty($email)) {
$sql .= "email=?, ";
$values[] = $email;
}
// Check if the bio field is filled
if (!empty($bio)) {
$sql .= "bio=?, ";
$values[] = $bio;
}
// Check if an image was uploaded
if (isset($_FILES["image"]) && $_FILES["image"]["error"] == 0) {
$image = $_FILES["image"]["name"];
$sql .= "image=?, ";
$values[] = $image;
// Upload image file to a directory
$targetDir = "uploads/"; // Change this to your desired directory
$targetFile = $targetDir . basename($_FILES["image"]["name"]);
move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile);
}
// Remove the trailing comma and space from the SQL query
$sql = rtrim($sql, ", ");
// Add the WHERE clause to specify the user
$sql .= " WHERE user_id=1";
// Prepare and execute the SQL query
$stmt = $conn->prepare($sql);
// Bind the values dynamically
$types = str_repeat("s", count($values));
$stmt->bind_param($types, ...$values);
// Check if the query execution was successful
if ($stmt->execute()) {
echo "Data has been updated successfully.";
} else {
echo "Error: " . $stmt->error;
}
// Close the statement
$stmt->close();
}
// Close the database connection
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Update Profile</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bootstrap.min.css">
<style>
body {
background: #384047;
font-family: sans-serif;
font-size: 10px
}
form {
background: #fff;
padding: 4em 4em 2em;
max-width: 400px;
margin: 20px auto 0;
box-shadow: 0 0 1em #222;
border-radius: 5px;
}
p {
margin: 0 0 3em 0;
position: relative;
}
label {
display: block;
font-size: 1.6em;
margin: 0 0 .5em;
color: #333;
}
input {
display: block;
box-sizing: border-box;
width: 100%;
outline: none
}
input[type="text"],
input[type="email"] {
background: #f5f5f5;
border: 1px solid #e5e5e5;
font-size: 1.6em;
padding: .8em .5em;
border-radius: 5px;
}
input[type="text"]:focus,
input[type="email"]:focus {
background: #fff
}
input[type="text"],
input[type="bio"] {
background: #f5f5f5;
border: 1px solid #e5e5e5;
font-size: 1.6em;
padding: .8em .5em;
border-radius: 5px;
}
input[type="text"]:focus,
input[type="bio"]:focus {
background: #fff
}
input[type="image"],
input[type="image"] {
background: #f5f5f5;
border: 1px solid #e5e5e5;
font-size: 1.6em;
padding: .8em .5em;
border-radius: 5px;
}
input[type="text"]:focus,
input[type="password"]:focus {
background: #fff
}
input[type="submit"] {
background: #2F558E;
box-shadow: 0 3px 0 0 #1D3C6A;
border-radius: 5px;
border: none;
color: #fff;
cursor: pointer;
display: block;
font-size: 2em;
line-height: 1.6em;
margin: 2em 0 0;
outline: none;
padding: .8em 0;
text-shadow: 0 1px #68B25B;
}
</style>
</head>
<body>
<?php include_once "navbar2.php"; ?>
<form action="#" method="post">
<p>
<label for="username">Username</label>
<input id="username" name="username" type="text">
</p>
<p>
<label for="password">Email</label>
<input id="email" name="email" type="email">
</p>
<p>
<label for="confirm_password">Bio</label>
<input id="bio" name="bio" type="bio">
</p>
<p>
<label for="image">Image</label>
<input id="image" name="image" type="file">
</p>
<p>
<input type="submit" value="SUBMIT" id="submit">
</p>
</form>
<script src="https://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/app.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>