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.

<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">
<td> Enter ISBN :</td>
<td> <input type="text" name="isbn"> </td>
<td> Enter Title :</td>
<td> <input type="text" name="title"> </td>
<td> Enter Author :</td>
<td> <input type="text" name="author"> </td>
<td> Enter Edition :</td>
<td> <input type="text" name="edition"> </td>
<td> Enter Publication: </td>
<td> <input type="text" name="publication"> </td>
 <input type="submit" value="submit">
 <input type="reset" value="Reset">



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"]; 
$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"); ?>

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

<a href=""> To search for the Book information click here </a>

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



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..