· 8 years ago · Mar 22, 2018, 12:36 PM
1<?php
2 $servername = "127.0.0.1";
3 $username = "root";
4 $password = "";
5 $dbname = "kontr";
6
7 // Create connection
8 $conn = new mysqli($servername, $username, $password, $dbname);
9 // Check connection
10 if ($conn->connect_error) {
11 die("Connection failed: " . $conn->connect_error);
12
13 }
14
15 // sql to create table
16 $sql = "CREATE TABLE IF NOT EXISTS books (
17 isbn INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
18 title VARCHAR(30),
19 published_year date,
20 genre VARCHAR(50),
21 author VARCHAR(50)
22 )";
23
24 if ($conn->query($sql)) {
25 //echo "Table books created successfully";
26 } else {
27 //echo "Error creating table: " . $conn->error;
28 }
29
30 if(isset($_GET['add'])){
31
32 $sql = "INSERT INTO books (title, published_year, genre, author)
33 VALUES ('".$_GET['title']."', '".$_GET['published_year']."', '".$_GET['genre']."', '".$_GET['author']."')";
34
35 if ($conn->query($sql)) {
36 echo "New record created successfully";
37 } else {
38 echo "Error: " . $sql . "<br>" . $conn->error;
39 }
40
41 $conn->close();
42 }
43
44 if(isset($_GET['search'])){
45
46 $sql = "select * from books where genre like '".$_GET['genre']."'";
47
48
49 if ($conn->query($sql)) {
50 $result = $conn->query($sql);
51
52 if ($result->num_rows > 0) {
53 while($row = $result->fetch_assoc()) {
54 echo "isbn: ".$row['isbn']." author: ".$row['author']."<br>";
55 }
56 } else {
57 echo "0 results";
58 }
59
60 } else {
61 echo "Error: " . $sql . "<br>" . $conn->error;
62 }
63
64 $conn->close();
65 }
66
67 if(isset($_GET['change'])){
68 $sql = "UPDATE books SET title='".$_GET['title']."' WHERE isbn=".$_GET['isbn'];
69
70 if ($conn->query($sql)) {
71 echo "Record updated successfully";
72 $sql = "select * from books where isbn=".$_GET['isbn'];
73
74
75 if ($conn->query($sql)) {
76 $result = $conn->query($sql);
77
78 if ($result->num_rows > 0) {
79 while($row = $result->fetch_assoc()) {
80 echo "isbn: ".$row['isbn']." Title: ".$row['title']."<br>";
81 }
82 } else {
83 echo "0 results";
84 }
85
86 } else {
87 echo "Error: " . $sql . "<br>" . $conn->error;
88 }
89 } else {
90 echo "Error updating record: " . $conn->error;
91 }
92 }
93
94 if(isset($_GET['sort'])){
95 $sql = "select * from books order by author ascxx";
96
97
98 if ($conn->query($sql)) {
99 $result = $conn->query($sql);
100
101 if ($result->num_rows > 0) {
102 while($row = $result->fetch_assoc()) {
103 echo "isbn: ".$row['isbn']." author: ".$row['author']."<br>";
104 }
105 } else {
106 echo "0 results";
107 }
108
109 } else {
110 echo "Error: " . $sql . "<br>" . $conn->error;
111 }
112
113 $conn->close();
114 }
115
116
117
118?>
119<h2>Add book</h2>
120<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
121 <label for="t" >Title</label>
122 <input id="t" type="text" name="title"></input>
123 <label for="p" >Pulished Year</label>
124 <input id="p" type="date" name="published_year"></input>
125 <label for="g" >Genre</label>
126 <input id="g" type="text" name="genre"></input>
127 <label for="a" >Author</label>
128 <input id="a" type="text" name="author"></input>
129 <button type="submit" name="add">add</button>
130</form>
131<h2>Get book by Genre</h2>
132<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
133 <label for="gs">Genre</label>
134 <input id="gs" type="text" name="genre"></input>
135 <button type="submit" name="search">search</button>
136</form>
137<h2>Change book Title</h2>
138<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
139 <label for="i" >ISBN</label>
140 <input id="i" type="number" name="isbn"></input>
141 <label for="ti" >Title</label>
142 <input id="ti" type="text" name="title"></input>
143 <button type="submit" name="change">change</button>
144</form>
145<h2>Sort book by Author</h2>
146<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
147 <button type="submit" name="sort">sort</button>
148</form>