-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.php
130 lines (124 loc) · 5.17 KB
/
update.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
<?php
session_start();
require_once 'components/db_connect.php';
require_once 'components/file_upload.php';
// if session is not set this will redirect to login page
if( !isset($_SESSION['adm']) && !isset($_SESSION['user']) ) {
header("Location: index.php");
exit;
}
$backBtn = '';
//if it is a user it will create a back button to home.php
if(isset($_SESSION["user"])){
$backBtn = "home.php";
}
//if it is a adm it will create a back button to dashboard.php
if(isset($_SESSION["adm"])){
$backBtn = "dashBoard.php";
}
//fetch and populate form
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "SELECT * FROM user WHERE id = {$id}";
$result = $connect->query($sql);
if ($result->num_rows == 1) {
$data = $result->fetch_assoc();
$f_name = $data['first_name'];
$l_name = $data['last_name'];
$email = $data['email'];
$date_birth = $data['date_of_birth'];
$picture = $data['picture'];
}
}
//update
$class = 'd-none';
if (isset($_POST["submit"])) {
$f_name = $_POST['first_name'];
$l_name = $_POST['last_name'];
$email = $_POST['email'];
$date_of_birth = $_POST['date_of_birth'];
$id = $_POST['id'];
//variable for upload pictures errors is initialized
$uploadError = '';
$pictureArray = file_upload($_FILES['picture']); //file_upload() called
$picture = $pictureArray->fileName;
if ($pictureArray->error === 0) {
($_POST["picture"] == "user.png") ?: unlink("pictures/{$_POST["picture"]}");
$sql = "UPDATE user SET first_name = '$f_name', last_name = '$l_name', email = '$email', date_of_birth = '$date_of_birth', picture = '$pictureArray->fileName' WHERE id = {$id}";
} else {
$sql = "UPDATE user SET first_name = '$f_name', last_name = '$l_name', email = '$email', date_of_birth = '$date_of_birth' WHERE id = {$id}";
}
if ($connect->query($sql) === true) {
$class = "alert alert-success";
$message = "The record was successfully updated";
$uploadError = ($pictureArray->error != 0) ? $pictureArray->ErrorMessage : '';
header("refresh:3;url=update.php?id={$id}");
} else {
$class = "alert alert-danger";
$message = "Error while updating record : <br>" . $connect->error;
$uploadError = ($pictureArray->error != 0) ? $pictureArray->ErrorMessage : '';
header("refresh:3;url=update.php?id={$id}");
}
}
$connect->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit User</title>
<?php require_once 'components/boot.php'?>
<style type= "text/css">
fieldset {
margin: auto;
margin-top: 100px;
width: 60% ;
}
.img-thumbnail{
width: 70px !important;
height: 70px !important;
}
</style>
</head>
<body>
<div class="container">
<div class="<?php echo $class; ?>" role="alert">
<p><?php echo ($message) ?? ''; ?></p>
<p><?php echo ($uploadError) ?? ''; ?></p>
</div>
<h2>Update</h2>
<img class='img-thumbnail rounded-circle' src='pictures/<?php echo $data['picture'] ?>' alt="<?php echo $f_name ?>">
<form method="post" enctype="multipart/form-data">
<table class="table">
<tr>
<th>First Name</th>
<td><input class="form-control" type="text" name="first_name" placeholder ="First Name" value="<?php echo $f_name ?>" /></td>
</tr>
<tr>
<th>Last Name</th>
<td><input class="form-control" type= "text" name="last_name" placeholder="Last Name" value ="<?php echo $l_name ?>" /></td>
</tr>
<tr>
<th>Email</th>
<td><input class="form-control" type="email" name="email" placeholder= "Email" value= "<?php echo $email ?>" /></td>
</tr>
<tr>
<th>Date of birth</th>
<td><input class="form-control" type="date" name="date_of_birth" placeholder= "Date of birth" value= "<?php echo $date_birth ?>" /></td>
</tr>
<tr>
<th>Picture</th>
<td><input class="form-control" type="file" name="picture" /></td>
</tr>
<tr>
<input type= "hidden" name= "id" value= "<?php echo $data['id'] ?>" />
<input type= "hidden" name= "picture" value= "<?php echo $picture ?>" />
<td><button name="submit" class="btn btn-success" type= "submit">Save Changes</button></td>
<td><a href= "<?php echo $backBtn?>"><button class="btn btn-warning" type="button">Back</button></a></td>
</tr>
</table>
</form>
</div>
</body>
</html>