-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathedit.php
105 lines (98 loc) · 2.32 KB
/
edit.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
<?php
// Include database connection file
include_once("config.php");
if(isset($_POST['update']))
{
// Retrieve record values
$id = $_POST['id'];
$name = $_POST['name'];
$age = $_POST['age'];
$email = $_POST['email'];
$nameErr = $ageErr = $emailErr = "";
// Check for empty fields
if(empty($name) || empty($age) || empty($email)) {
if(empty($name)) {
$nameErr = "* required";
}
if(empty($age)) {
$ageErr = "* required";
}
if(empty($email)) {
$emailErr = "* required";
}
} else {
// Execute UPDATE
$stmt = $pdo->prepare("UPDATE contacts SET name = ?, age = ?, email = ? WHERE id = ?");
$stmt->execute([$name, $age, $email, $id]);
// Redirect to home page (index.php)
header("Location: index.php");
}
}
else if (isset($_POST['cancel'])) {
// Redirect to home page (index.php)
header("Location: index.php");
}
?>
<?php
//class Contact
// Retrieve id value from querystring parameter
$id = $_GET['id'];
// Get contact by id
$stmt = $pdo->prepare("SELECT * FROM contacts WHERE id = ?");
$stmt->execute([$id]);
$arr = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$arr) {
printf($arr);
exit($arr);
}
else {
//foreach($arr as $row)
//{
$name = $arr['name'];
$age = $arr['age'];
$email = $arr['email'];
//}
}
?>
<html>
<head>
<title>Edit Contact</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<form name="form1" method="post" action="edit.php?id=<?php echo $id ?>">
<table>
<tr>
<td>Name</td>
<td>
<input type="text" name="name" value="<?php echo $name;?>">
<span class="error"><?php echo $nameErr;?></span>
</td>
</tr>
<tr>
<td>Age</td>
<td>
<input type="text" name="age" value="<?php echo $age;?>">
<span class="error"><?php echo $ageErr;?></span>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type="text" name="email" value="<?php echo $email;?>">
<span class="error"><?php echo $emailErr;?></span>
</td>
</tr>
<tr>
<td>
<input class="cancel" type="submit" name="cancel" value="Cancel">
</td>
<td>
<input type="submit" name="update" value="Update">
<input type="hidden" name="id" value=<?php echo $_GET['id'];?>>
</td>
</tr>
</table>
</form>
</body>
</html>