-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditUser3.php
More file actions
215 lines (200 loc) · 8.31 KB
/
Copy patheditUser3.php
File metadata and controls
215 lines (200 loc) · 8.31 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
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
<?php
$pdo = new PDO('mysql:host=localhost; port=3306; dbname=misc', 'kings', 'zap');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
require_once "util.php";
session_start();
//check if the user is logged in
if(! isset($_SESSION['user_id']) && !isset($_SESSION['email'])) {
die('ACCESS DENIED');
return false;
}
//check if the get request is set before you load up the profile
if(!isset($_REQUEST['profile_id'])) {
$_SESSION['error'] = "Missing profile ID";
header('location: user_acc3.php');
return;
}
$sql = "SELECT * FROM profile WHERE profile_id = :pro AND user_id = :use";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':pro' => $_REQUEST['profile_id'], ':use' => $_SESSION['user_id']));
$profile = $stmt->fetch(PDO::FETCH_ASSOC);
if($profile === false) {
$_SESSION['error'] = "Could not load profile";
header('location: user_acc3.php');
return;
}
//redirect the cancel button
if(isset($_POST['Cancel'])) {
header('location: user_acc3.php');
return;
}
//validate incoming data if the user is logged in
if (isset($_POST['First_name']) && isset($_POST['Last_name']) && isset($_POST
['Email']) && isset($_POST['Headline']) && isset($_POST['Summary'])) {
//validate profiles
$msg = validateProfile();
if(is_string($msg)) {
$_SESSION['error'] = $msg;
header('location: editUser3.php?profile_id='.$_REQUEST['profile_id']);
return;
}
//validate education data
$Edu = validateEdu();
if(is_string($Edu)) {
$_SESSION['error'] = $Edu;
header('location: editUser3.php?profile_id='.$_REQUEST['profile_id']);
return;
}
//validate position data
$Pos = validatePos();
if(is_string($Pos)) {
$_SESSION['error'] = $Pos;
header('location: editUser3.php?profile_id='.$_REQUEST['profile_id']);
return;
}
//update the data
$stmt = $pdo->prepare("UPDATE profile SET First_name = :fn, Last_name = :ln,
Email = :em, Headline = :hdl, Summary = :sm WHERE profile_id = :prof AND
user_id = :uid");
$stmt->execute(array(':fn' => $_POST['First_name'],
':ln' => $_POST['Last_name'],
':em' => $_POST['Email'],
':hdl' => $_POST['Headline'],
':sm' => $_POST['Summary'],
':prof' => $_REQUEST['profile_id'],
':uid' => $_SESSION['user_id'])
);
//clear out education entries before inserting entries
$stmt = $pdo->prepare("DELETE FROM education WHERE profile_id = :pid");
$stmt->execute(array(':pid' => $_REQUEST['profile_id']));
//insert into education
insertEdu($pdo, $_REQUEST['profile_id']);
//clear out the old position entries
$stmt = $pdo->prepare("DELETE FROM position WHERE profile_id = :pidd");
$stmt->execute(array(':pidd' => $_REQUEST['profile_id']));
//insert the positions
insertPos($pdo, $_REQUEST['profile_id']);
$_SESSION['success'] = "profile updated";
header('location: user_acc3.php');
return;
}
//load education and position entries
$schools = loadEdu($pdo, $_REQUEST['profile_id']);
$positions = loadPos($pdo, $_REQUEST['profile_id']);
?>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
form, h3 {
margin: 0 auto;
margin-left:10%;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous">
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" type="text/css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
</head>
<body>
<h3>Editing profile, position and education for <?php echo htmlentities($_SESSION['name']); ?></h3>
<?= flashMessage() ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="profile_id" value="<?= htmlentities($_REQUEST['profile_id']); ?>">
<p>First Name: <input type="text" size="35px" name="First_name"
value="<?= htmlentities($profile['First_name']); ?>"></p>
<p>Last Name: <input type="text" size="35px" name="Last_name"
value="<?= htmlentities($profile['Last_name']); ?>"></p>
<p>Email: <input type="email" size="35px" name="Email"
value="<?= htmlentities($profile['Email']); ?>"></p>
<p>Headline: <input type="text" size="35px" name="Headline"
value="<?= htmlentities($profile['Headline']); ?>"></p>
<p>Summary: <br/><textarea name="Summary" id="" cols="40" rows="10">
<?= htmlentities($profile['Summary']); ?>
</textarea></p>
<?php
//load education entries
echo '<p>Education: <input type="submit" id="addEdu" value="+"></p>';
$countEdu = 0;
echo '<div id="edu_fields">';
foreach($schools as $school) {
if(count($school) > 0) {
$countEdu++;
echo '<div id="Edu'.$countEdu.'">
<p>Year: <input type="number" name="edu_year'.$countEdu.'" value="'.$school['year'].'">
<input type="button" value="-" onclick="$(\'#Edu'.$countEdu.'\').remove();return false;"></p>
<p>School: <input type="text" size="30px" name="edu_school'.$countEdu.'" value="'.$school['name'].'" class="sch"></p>
</div>';
}
}
echo '</div>';
//load position entries
$countPos = 0;
echo '<p>Position: <input type="submit" id="addPos" value="+"></p>';
echo '<div id="pos_fields">';
foreach($positions as $position) {
if(count($position) > 0) {
$countPos++;
echo '<div id="pos'.$countPos.'">
<p>Year: <input type="number" name="year'.$countPos.'" value="'.$position['year'].'">
<input type="button" value="-" onclick="$(\'#pos'.$countPos.'\').remove();return false;"></p>
<textarea name="desc'.$countPos.'" rows="10" cols="40px">'.$position['description'].'</textarea>
</div>';
}
}
echo '</div><br/>';
?>
<input type="submit" value="Update">
<input type="submit" value="Cancel" name="Cancel">
</form>
<!-- to add more education -->
<script>
countEdu = <?= $countEdu ?>;
countPos = <?= $countPos ?>;
$(document).ready(function(){
$('#addEdu').click(function(event){
event.preventDefault();
if(countEdu > 3) {
alert('maximum number of education reached');
return;
}
countEdu++;
$('#edu_fields').append(
'<div id="Edu'+countEdu+'"> \
<p>Year: <input type="number" name="edu_year'+countEdu+'" value=""> \
<input type="button" value="-" onclick="$(\'#Edu'+countEdu+'\').remove();return false;"></p> \
<p>School: <input type="text" size="30px" name="edu_school'+countEdu+'" value="" class="sch"></p> \
</div>'
);
$('.sch').autocomplete({
source: "school.php"
});
})
})
//to add more positions
$(document).ready(function(){
$('#addPos').click(function(event){
event.preventDefault();
if(countPos > 3) {
alert('maximum number of position reached');
return;
}
countPos++;
$('#pos_fields').append(
'<div id="pos'+countPos+'"> \
<p>Year: <input type="number" name="year'+countPos+'" value=""> \
<input type="button" value="-" onclick="$(\'#pos'+countPos+'\').remove();return false;"></p> \
<textarea name="desc'+countPos+'" rows="10" cols="40"></textarea> \
</div>'
);
})
})
</script>
</body>
</html>