Advertisement
Guest User

Untitled

a guest
May 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. <?php // connect.php allows connection to the database
  2.  
  3. require 'connect.php'; //using require will include the connect.php file each time it is called.
  4.  
  5. if (isset($_POST['id']) &&
  6. isset($_POST['title']) &&
  7. isset($_POST['author']) &&
  8. isset($_POST['isbn'])
  9. )
  10.  
  11.  
  12. {
  13. $id = assign_data($conn, 'id');
  14. $title = assign_data($conn, 'title');
  15. $author = assign_data($conn, 'author');
  16. $ISBN = assign_data($conn, 'isbn');
  17.  
  18.  
  19. $query = "INSERT INTO Library VALUES ('$id', '$title', '$author', '$isbn')";
  20.  
  21. $result = $conn->query($query);
  22. if (!$result) echo "<br><br>INSERT failed: $query<br>" .
  23.  
  24. $conn->error . "<br><br>";
  25. }
  26.  
  27. // else {
  28. // print "<br><br> You did not fill all required <br><br>";
  29. // }
  30.  
  31.  
  32. echo <<<_END
  33. <form action=" " method="post">
  34.  
  35. Book id <input type="text" name="id"> <br><br>
  36. Book title <input type="text" name="title"> <br><br>
  37. Author name <input type="text" name="author"> <br><br>
  38. ISBN <input type ="text" name="isbn"> <br><br>
  39.  
  40. <input type="submit" value="ADD RECORD">
  41.  
  42. </form>
  43. _END;
  44.  
  45.  
  46.  
  47. function assign_data($conn, $var)
  48. {
  49. return $conn->real_escape_string($_POST[$var]);
  50. }
  51. ?>
  52.  
  53. <?php
  54.  
  55. $query = "SELECT * FROM Library";
  56. $result = $conn->query($query);
  57. if (!$result) die ("Database access failed: " . $conn->error);
  58.  
  59. $rows = $result->num_rows;
  60. ?>
  61. <b>Here is your Books list</b>
  62.  
  63. <table>
  64. <tr>
  65. <th>Book ID</th><br>
  66.  
  67. <th>Title</th><br>
  68.  
  69. <th>Author</th><br>
  70.  
  71. <th>ISBN</th><br>
  72.  
  73. </tr>
  74. <?php
  75.  
  76. $rows = $result->num_rows;
  77.  
  78. for ($i = 0 ; $i < $rows ; ++$i)
  79. {
  80. $result->data_seek($i);
  81. echo 'Book id: ' . $result->fetch_assoc()['id'] . '<br>';
  82. $result->data_seek($i);
  83. echo 'Title: ' . $result->fetch_assoc()['title'] . '<br>';
  84. $result->data_seek($i);
  85. echo 'Author: ' . $result->fetch_assoc()['author'] . '<br><br>';
  86. $result->data_seek($i);
  87. echo 'ISBN: ' . $result->fetch_assoc()['isbn'] . '<br>';
  88. }
  89.  
  90. /**
  91. * Used for table representation
  92. */
  93.  
  94. /**if($result->num_rows >0)
  95. {
  96. echo"The books list:<br><br>";
  97. while($row=$result->fetch_assoc())
  98. {
  99. echo"<tr>";
  100. echo"<td>".$row["field name"]."</td>";
  101. echo"<td>".$row["field name"]."</td>";
  102. echo"<td>".$row["field name"]."</td>";
  103. echo"</tr>";
  104. }
  105. }
  106.  
  107. else {
  108. echo"0 results";
  109. }
  110. */
  111. $result->close();
  112. $conn->close();
  113. ?>
  114.  
  115. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement