Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <meta charset = "utf-8">
- <link rel="stylesheet" type="text/css" href="mystyle.css">
- <style>
- </style>
- </head>
- <body>
- <style>
- </style>
- <?php
- require 'connect.php'; //using require will include the connect.php file each time it is called.
- /*
- Server side validation function.
- */
- if(strlen($_POST['id']) > 20 ) { echo"ID too long!";
- return false;
- }
- else if(strlen($_POST['title']) > 30 )
- {
- echo"Book title too long!";
- return false;
- }
- else if(strlen($_POST['author']) > 10)
- {
- echo"No author submitted!";
- return false;
- }
- else if(strlen($_POST['year']) > 4)
- {
- echo"Year is too long!";
- return false;
- }
- else if(strlen($_POST['isbn']) ? ) { echo "ISBN not valid!";
- return false;
- }
- function validation_check()
- {
- // Validation code for the different fields can go here.
- if(empty($_POST['id']))
- {
- echo"No id submitted!";
- return false;
- }
- else if(empty($_POST['title']))
- {
- echo"No title submitted!";
- return false;
- }
- else if(empty($_POST['author']))
- {
- echo"No author submitted!";
- return false;
- }
- else if(empty($_POST['year']))
- {
- echo"No year submitted!";
- return false;
- }
- else if(empty($_POST['isbn']))
- {
- echo"No ISBN submitted!";
- return false;
- }
- return true;
- }
- if (isset($_POST['id']) &&
- isset($_POST['title']) &&
- isset($_POST['author']) &&
- isset($_POST['year']) &&
- isset($_POST['isbn'])
- )
- {
- $id = assign_data($conn, 'id');
- $title = assign_data($conn, 'title');
- $author = assign_data($conn, 'author');
- $Year = assign_data($conn, 'year');
- $ISBN = assign_data($conn, 'isbn');
- // validation process
- $validation_passed = validation_check();
- if($validation_passed == TRUE)
- {
- echo"Record has been successfully updated";
- }
- else{
- echo"Error record hasn't been updated properly";
- }
- }
- ?>
- <form action=" " method="post">
- <div class = "buttons">
- <label for="id"> Book id: </label> <br>
- <input type="text" name="id" value = ""> <br>
- <input type="button" class="buttons" value="Delete Record"> <br>
- <label for="title"> Book Title: </label> <br>
- <input type="text" name="title" value = ""> <br>
- <label for="author"> Author name:</label> <br>
- <input type="text" name="author" value = ""> <br>
- <label for="author"> Year:</label> <br>
- <input type="year" name="year" value = ""> <br>
- <label for="author">ISBN:</label> <br>
- <input type="text" name="isbn" value = ""> <br>
- <input type="submit" value="add record">
- <br>
- <p>
- <?php
- if(isset($validation_error))
- { echo "<p><em>$validation_error</em></p>";} //show validation error messages if they exists.?></p>
- </form>
- <?php
- function assign_data($conn, $var)
- {
- return $conn->real_escape_string($_POST[$var]);
- }
- $query = "SELECT * FROM Library";
- $result = $conn->query($query);
- if (!$result) die ("Database access failed: " . $conn->error);
- $rows = $result->num_rows;
- ?>
- <p>Here is your Books list</p>
- <table>
- <tr>
- <th>Book id</th>
- <th>Title</th>
- <th>Author</th>
- <th>Year</th>
- <th>ISBN</th>
- </tr>
- <?php
- if ($result->num_rows >0)
- {
- while($row = $result->fetch_assoc())
- {
- echo "<tr>";
- echo "<td>".$row["id"]."</td>";
- echo "<td>".$row["title"]."</td>";
- echo "<td>".$row["author"]."</td>";
- echo "<td>".$row["year"]."</td>";
- echo "<td>".$row["isbn"]."</td>";
- echo "</tr>";
- }
- }
- else
- {
- echo "0 results";
- }
- ?>
- </table>
- <?php
- $conn->close();
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement