Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php // connect.php allows connection to the database
- require 'connect.php'; //using require will include the connect.php file each time it is called.
- if (isset($_POST['id']) &&
- isset($_POST['title']) &&
- isset($_POST['author']) &&
- isset($_POST['isbn'])
- )
- {
- $id = assign_data($conn, 'id');
- $title = assign_data($conn, 'title');
- $author = assign_data($conn, 'author');
- $ISBN = assign_data($conn, 'isbn');
- $query = "INSERT INTO Library VALUES ('$id', '$title', '$author', '$isbn')";
- $result = $conn->query($query);
- if (!$result) echo "<br><br>INSERT failed: $query<br>" .
- $conn->error . "<br><br>";
- }
- // else {
- // print "<br><br> You did not fill all required <br><br>";
- // }
- echo <<<_END
- <form action=" " method="post">
- Book id <input type="text" name="id"> <br><br>
- Book title <input type="text" name="title"> <br><br>
- Author name <input type="text" name="author"> <br><br>
- ISBN <input type ="text" name="isbn"> <br><br>
- <input type="submit" value="ADD RECORD">
- </form>
- _END;
- function assign_data($conn, $var)
- {
- return $conn->real_escape_string($_POST[$var]);
- }
- ?>
- <?php
- $query = "SELECT * FROM Library";
- $result = $conn->query($query);
- if (!$result) die ("Database access failed: " . $conn->error);
- $rows = $result->num_rows;
- ?>
- <b>Here is your Books list</b>
- <table>
- <tr>
- <th>Book ID</th><br>
- <th>Title</th><br>
- <th>Author</th><br>
- <th>ISBN</th><br>
- </tr>
- <?php
- $rows = $result->num_rows;
- for ($i = 0 ; $i < $rows ; ++$i)
- {
- $result->data_seek($i);
- echo 'Book id: ' . $result->fetch_assoc()['id'] . '<br>';
- $result->data_seek($i);
- echo 'Title: ' . $result->fetch_assoc()['title'] . '<br>';
- $result->data_seek($i);
- echo 'Author: ' . $result->fetch_assoc()['author'] . '<br><br>';
- $result->data_seek($i);
- echo 'ISBN: ' . $result->fetch_assoc()['isbn'] . '<br>';
- }
- /**
- * Used for table representation
- */
- /**if($result->num_rows >0)
- {
- echo"The books list:<br><br>";
- while($row=$result->fetch_assoc())
- {
- echo"<tr>";
- echo"<td>".$row["field name"]."</td>";
- echo"<td>".$row["field name"]."</td>";
- echo"<td>".$row["field name"]."</td>";
- echo"</tr>";
- }
- }
- else {
- echo"0 results";
- }
- */
- $result->close();
- $conn->close();
- ?>
- </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement