How to Insert HTML data to MySQL database using PHP

By | December 29, 2016

This post explains on how to insert HTML data to MySQL database using PHP. This is an application to store the details of books. This application is using a simple HTML form to submit book details. After this, a PHP code is written to read all the input details from HTML form and insert them into a MySQL database.

Question: Develop an application to accept book information viz. Accession number (ISBN), Title, Author, Edition, and Publication from a web page and store the information in a database.

<html>
<body bgcolor="aabbcc">
 
<h3>Program to accept the Book-Information</h3>

 <!--Once the form is submitted the details are forwared to InsertBookDetails.php -->
<form action="http://localhost/InsertBookDetails.php" method="get">

<table border="0">
<tr>
<td> Enter ISBN :</td>
<td> <input type="text" name="isbn"> </td>
</tr>
<tr>
<td> Enter Title :</td>
<td> <input type="text" name="title"> </td>
</tr>
<tr>
<td> Enter Author :</td>
<td> <input type="text" name="author"> </td>
</tr>
<tr>
<td> Enter Edition :</td>
<td> <input type="text" name="edition"> </td>
</tr>
<tr>
<td> Enter Publication: </td>
<td> <input type="text" name="publication"> </td>
</tr>
<tr>
<td>
 <input type="submit" value="submit">
 <input type="reset" value="Reset">
</td>
</tr>
</table>

 </form>

 </body>
 </html></pre>
<pre>

The BookDeatils.html page looks like as shown in the below image:



The following PHP code will accept all the inputs submitted through HTML page and makes a connection to MySQL database and details are inserted into the database.



<? $isbn=$_REQUEST["isbn"]; 
$title=$_REQUEST["title"]; 
$author=$_REQUEST["author"]; 
$edition=$_REQUEST["edition"]; 
$publication=$_REQUEST["publication"]; 
$mysql = mysql_connect("localhost","root") or die("cannot connect"); 
mysql_select_db("books") or die("cannot select"); 
mysql_query("insert into book_info(isbn,title,author,edition,publication) values('$isbn','$title','$author','$edition','$publication')") or die ("query failed to insert"); ?>

<html>
<head>
<title> Insert book details </title>
</head>
<body bgcolor="aabbcc">

<a href=" http://krazytech.com/programs/search-and-display-feature-in-php-mysql"> To search for the Book information click here </a>

<h4> Book information has been inserted into MySQL database </h4>

</body>
</html>

 

If InsertBookDetails.php code is executed successfully then you should be able to see the following page and also you can verify the details in the database.

Insert HTML data to MySQL database using PHP





Author: Ravi Bandakkanavar

A Techie, Blogger, Web Designer, Programmer by passion who aspires to learn new Technologies every day. It has been 6 years since I have been publishing articles and enjoying every bit of it. I want to share knowledge and build a great community with people like you.

Suggested read for you:

Did it help? Comment here..