-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_book.php
executable file
·47 lines (40 loc) · 1.21 KB
/
insert_book.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
<!DOCTYPE html>
<html>
<head>
<title>Book-O-Rama Book Entry Results</title>
</head>
<body>
<h1>Book-O-Rama Book Entry Results</h1>
<?php
if (!isset($_POST['ISBN']) || !isset($_POST['Author'])
|| !isset($_POST['Title']) || !isset($_POST['Price'])) {
echo "<p>You have not entered all the required details.<br />
Please go back and try again.</p>";
exit;
}
// create short variable names
$isbn=$_POST['ISBN'];
$author=$_POST['Author'];
$title=$_POST['Title'];
$price=$_POST['Price'];
$price = doubleval($price);
@$db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');
if (mysqli_connect_errno()) {
echo "<p>Error: Could not connect to database.<br/>
Please try again later.</p>";
exit;
}
$query = "INSERT INTO Books VALUES (?, ?, ?, ?)";
$stmt = $db->prepare($query);
$stmt->bind_param('sssd', $isbn, $author, $title, $price);
$stmt->execute();
if ($stmt->affected_rows > 0) {
echo "<p>Book inserted into the database.</p>";
} else {
echo "<p>An error has occurred.<br/>
The item was not added.</p>";
}
$db->close();
?>
</body>
</html>