-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadd.php
79 lines (75 loc) · 1.7 KB
/
add.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
<?php
// Include database connection file
include_once("config.php");
if(isset($_POST['update']))
{
// Retrieve record values
$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 {
// Insert new contact
$stmt = $pdo->prepare("INSERT INTO contacts (name,age,email) VALUES(?, ?, ?)");
$stmt->execute([$name, $age, $email]);
// 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");
}
?>
<html>
<head>
<title>Edit Contact</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<form name="form1" method="post" action="add.php">
<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">
</td>
</tr>
</table>
</form>
</body>
</html>