Ex No: 6 Write a program to Create Book Master Form in PHP
Aim: To create a book master form to store and display book details using php
Algorithm
Step1: Design a webpage to get Book Name, Author Name and Price. Add Navigation menu in the webpage to view saved data.
Step2: On form submit check if Book name is set and not empty
   A) Get Book Name, Author Name and Price values.
   B) Open file “books.txt”.
   C) If file is available.
    i) Save the details in table format.
    ii) close the file.
   D) Else display fie not found.
Ex6_view.php
Step 1: Design a web page with navigation to add books data page.
Step 2: Open file “books.txt”.
Step 3: If file is available.
   A) Read the file content.
   B) Display the data in a table.
Step 3: Else display file not found.

index.html


<!DOCTYPE html>
<html>
<head>
 <title>Book Master Form</title>
</head>
<body>
<h3><u>Book Master Form</u></h3>
 <div style="float: left; width: 100px;">
  <table style=" border-style: dotted;">
   <tr>	<td>Menu</td> </tr>
   <tr>	<td><a href="#">Add Books</a></td> </tr>
   <tr>	<td><a href="ex6_view.php">View Books</a></td> </tr>
  </table>
 </div>
 <div style="float: left; width: 500px;">
  <form id="strprocess" name="strprocess" method="POST" action="ex6.php">
   <table>
    <tr>
     <td colspan="2">Enter Book Details</td>
    </tr>
    <tr>
     <td>Book Name:</td>
     <td><input  type="text" name="book_name"/></td>
    </tr>   	
    <tr>
     <td>Author Name:</td>
     <td><input  type="text" name="auth_name"/></td>
    </tr>
    <tr>
     <td>Price:</td>
     <td><input  type="text" name="book_price"/></td>
    </tr>
    <tr>
     <td><input type="submit" value="Add Book"/></td>
     <td><input type="reset"/></td>
    </tr>
   </table>
  </form>
 </div>  
</body>
</html>

ex6.php


<!DOCTYPE html>
<html>
<head>
 <title>Book Master Form</title>
</head>
<body>
<h3><u>Book Details Stored</u></h3>
 <div style="float: left; width: 100px;">
  <table style=" border-style: dotted;">
   <tr>	<td>Menu</td> </tr>
   <tr>	<td><a href="#">Add Books</a></td> </tr>
   <tr>	<td><a href="ex6_view.php">View Books</a></td> </tr>
  </table>
 </div>
<?php
 
  #starting point of the php program
  if((isset($_POST["book_name"]) && $_POST["book_name"]!=""))
  {
   $bookname = $_POST["book_name"];
   $author = $_POST["auth_name"];
   $price = $_POST["book_price"];
  	
   $filename = "books.txt";
   $file = fopen($filename,"a+");
   if($file != FALSE)
   {
     $text = "<tr><td>".$bookname."</td><td>".$author."</td><td>".$price."</td></tr>";
     fwrite($file,$text);
     echo "<h3><u>Details Successfully Added</u></h3>";
     fclose($file);
   }
   else
   {
     echo "<h3><u>File Not Found</u></h3>";
   }
  }
 ?>
 </body>
</html>

ex6_view.php


<!DOCTYPE html>
<html>
<head>
 <title>Book Master Details</title>
</head>
<body>
 <?php
  $filename = "books.txt";
  $file = fopen($filename,"a+");
  if($file != FALSE)
  {	
    $filesize = filesize( $filename );
    $filetext = fread( $file, $filesize );
    fclose( $file );	
 ?>
 <h3><u>Book Master Details</u></h3>
 <div style="float: left; width: 100px;">
  <table style=" border-style: dotted;">
   <tr>	<td>Menu</td> </tr>
   <tr>	<td><a href="index.html">Add Books</a></td> </tr>
   <tr>	<td><a href="#">View Books</a></td> </tr>
  </table>
 </div>
  
  <table style="border: 2px solid black">
   <tr>
    <th width="50%">Book Name</th>
    <th width="40%">Author Name</th>
    <th width="10%">Price</th>
   </tr>
   <?php
   	echo $filetext;
   ?>
  </table>
 </div> 
 <?php 
  }
  else
  {
   echo "<h3><u>File Not Found</u></h3>";
  }
 ?>
 </body>
</html>
Result: Thus a webpage is designed using php to create book master form.