forked from dicodingacademy/a83-macd-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
86 lines (84 loc) · 3.14 KB
/
index.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
<html>
<head>
<Title>Registration Form</Title>
<style type="text/css">
body { background-color: #fff; border-top: solid 10px #000;
color: #333; font-size: .85em; margin: 20; padding: 20;
font-family: "Segoe UI", Verdana, Helvetica, Sans-Serif;
}
h1, h2, h3,{ color: #000; margin-bottom: 0; padding-bottom: 0; }
h1 { font-size: 2em; }
h2 { font-size: 1.75em; }
h3 { font-size: 1.2em; }
table { margin-top: 0.75em; }
th { font-size: 1.2em; text-align: left; border: none; padding-left: 0; }
td { padding: 0.25em 2em 0.25em 0em; border: 0 none; }
</style>
</head>
<body>
<h1>Register here!</h1>
<p>Fill in your name and email address, then click <strong>Submit</strong> to register.</p>
<form method="post" action="index.php" enctype="multipart/form-data" >
Name <input type="text" name="name" id="name"/></br></br>
Email <input type="text" name="email" id="email"/></br></br>
Job <input type="text" name="job" id="job"/></br></br>
<input type="submit" name="submit" value="Submit" />
<input type="submit" name="load_data" value="Load Data" />
</form>
<?php
try{
$conn = new PDO("sqlsrv:server = tcp:d1code.database.windows.net,1433; Database =<db name>", "<db username>", "<db password>");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e){
print("Error connecting to SQL Server.");
die(print_r($e));
}
if (isset($_POST['submit'])) {
try {
$name = $_POST['name'];
$email = $_POST['email'];
$job = $_POST['job'];
$date = date("Y-m-d");
// Insert data
$sql_insert = "INSERT INTO Registration (name, email, job, date)
VALUES (?,?,?,?)";
$stmt = $conn->prepare($sql_insert);
$stmt->bindValue(1, $name);
$stmt->bindValue(2, $email);
$stmt->bindValue(3, $job);
$stmt->bindValue(4, $date);
$stmt->execute();
} catch(Exception $e) {
echo "Failed: " . $e;
}
echo "<h3>Your're registered!</h3>";
} else if (isset($_POST['load_data'])) {
try {
$sql_select = "SELECT * FROM Registration";
$stmt = $conn->query($sql_select);
$registrants = $stmt->fetchAll();
if(count($registrants) > 0) {
echo "<h2>People who are registered:</h2>";
echo "<table>";
echo "<tr><th>Name</th>";
echo "<th>Email</th>";
echo "<th>Job</th>";
echo "<th>Date</th></tr>";
foreach($registrants as $registrant) {
echo "<tr><td>".$registrant['name']."</td>";
echo "<td>".$registrant['email']."</td>";
echo "<td>".$registrant['job']."</td>";
echo "<td>".$registrant['date']."</td></tr>";
}
echo "</table>";
} else {
echo "<h3>No one is currently registered.</h3>";
}
} catch(Exception $e) {
echo "Failed: " . $e;
}
}
?>
</body>
</html>