-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignGuestBook.php
63 lines (62 loc) · 2.15 KB
/
SignGuestBook.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sign Guest Book</title>
</head>
<body>
<?php
if (empty($_POST['first_name']) || empty($_POST['last_name'])) {
echo "<p>You must enter your first and last name! Click your browser's Back button to return to the Guest Book form.</p>";
} else {
$DBConnect = mysqli_connect("127.0.0.1", "root", "");
if ($DBConnect === FALSE) {
echo "<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_errno() . ": "
. mysqli_error() . "</p>";
} else {
$DBName = "guestbook";
if (!mysqli_select_db($DBConnect, $DBName)) {
$SQLstring = "CREATE DATABASE $DBName";
$QueryResult = mysqli_query($DBConnect,
$SQLstring);
if ($QueryResult === FALSE) {
echo "<p>Unable to execute the query.</p>"
. "<p>Error code "
. mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect) . "</p>";
} else {
echo "<p>You are the first visitor!</p>";
}
}
mysqli_select_db($DBConnect, $DBName);
$TableName = "visitors";
$SQLstring = "SHOW TABLES LIKE '$TableName'";
$QueryResult = mysqli_query($DBConnect, $SQLstring);
if (mysqli_num_rows($QueryResult) == 0) {
$SQLstring = "CREATE TABLE $TableName(countID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, last_name VARCHAR(40),first_name VARCHAR(40))";
$QueryResult = mysqli_query($DBConnect, $SQLstring);
if ($QueryResult === FALSE) {
echo "<p>Unable to create the table.</p>"
. "<p>Error code "
. mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect) . "</p>";
}
}
$LastName = stripslashes($_POST['last_name']);
$FirstName = stripslashes($_POST['first_name']);
$SQLstring2 = "INSERT INTO $TableName VALUES(NULL,'$LastName', '$FirstName')";
$QueryResult2 = mysqli_query($DBConnect, $SQLstring2);
if ($QueryResult2 === FALSE) {
echo "<p>Unable to execute the query.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect) . "</p>";
} else {
echo "<h1>Thank you for signing our guestbook!</h1>";
}
mysqli_close($DBConnect);
}
}
?>
</body>
</html>