-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.php
More file actions
89 lines (71 loc) · 2.34 KB
/
update.php
File metadata and controls
89 lines (71 loc) · 2.34 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
<?php
include "config.php";
// if the form's update button is clicked, we need to process the form
if (isset($_POST['update'])) {
$firstname = $_POST['firstname'];
$user_id = $_POST['user_id'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$password = $_POST['password'];
$gender = $_POST['gender'];
// write the update query
$sql = "UPDATE `users` SET `firstname`='$firstname',`lastname`='$lastname',`email`='$email',`password`='$password',`gender`='$gender' WHERE `id`='$user_id'";
// execute the query
$result = $conn->query($sql);
if ($result == TRUE) {
echo "Record updated successfully.";
}else{
echo "Error:" . $sql . "<br>" . $conn->error;
}
}
// if the 'id' variable is set in the URL, we know that we need to edit a record
if (isset($_GET['id'])) {
$user_id = $_GET['id'];
// write SQL to get user data
$sql = "SELECT * FROM `users` WHERE `id`='$user_id'";
//Execute the sql
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$first_name = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];
$password = $row['password'];
$gender = $row['gender'];
$id = $row['id'];
}
?>
<h2>User Update Form</h2>
<form action="" method="post">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" value="<?php echo $first_name; ?>">
<input type="hidden" name="user_id" value="<?php echo $id; ?>">
<br>
Last name:<br>
<input type="text" name="lastname" value="<?php echo $lastname; ?>">
<br>
Email:<br>
<input type="email" name="email" value="<?php echo $email; ?>">
<br>
Password:<br>
<input type="password" name="password" value="<?php echo $password; ?>">
<br>
Gender:<br>
<input type="radio" name="gender" value="Male" <?php if($gender == 'Male'){ echo "checked";} ?> >Male
<input type="radio" name="gender" value="Female" <?php if($gender == 'Female'){ echo "checked";} ?>>Female
<br><br>
<input type="submit" value="Update" name="update">
</fieldset>
</form>
</body>
</html>
<?php
} else{
// If the 'id' value is not valid, redirect the user back to view.php page
header('Location: view.php');
}
}
?>
<!-- build by Bryan Booij 11 feb 13:45 -->