ARTICLE

Database Connectivity (Insert and Select) With MySQL in PHP

Posted by Vineet Kumar Saini Articles | PHP February 28, 2012
This article explains how to insert and select data from database into a HTML table in PHP.
Reader Level:

This article explains how to insert and select data from a database into a HTML table in PHP. Using an insert statement you can insert data into a database. Using a select statement you can select the entire data from a database into a HTML table.

First of all we will create a database.

Create The Database

<html>
<
body bgcolor="pink">
<center>
<?php
$con = mysql_connect ("localhost","root","");
$db=
"MCN";
if (!$con)
  {
 
die ('Could not connect: ' . mysql_error());
  }
if (mysql_query ("CREATE DATABASE $db",$con))
  {
 
echo "Your Database Created Which Name is : $db";
  }
else
 
{
 
echo "Error creating database: " . mysql_error();
  }
mysql_close ($con);
?>
</center>
}</
body>
</html>

Output

In the above code, we created a database named MCN. Now we will create a table in the MCN database.

image1.jpg

Create The Table

<html>
<
body bgcolor="pink">
<center>
<?php
$con = mysql_connect ("localhost","root","");
if (!$con)
  {
 
die ('Could not connect: ' . mysql_error());
  }
mysql_select_db (
"MCN", $con);
$sql =
"CREATE TABLE EMPLOYEE
(
Name varchar(50),
Designation varchar(50),
Sal int,
Qualification varchar(50)
)"
;
mysql_query($sql,$con);
echo "Your  table created which is as follows";
mysql_close($con);
?>
<table bgcolor="skyblue" border="2">
<tr>
<
td colspan=2 align="center">Employee Table</td>
</tr>
<
td>Name</td><td><input type="text" name="txt1"></td>
</tr>
<
tr>
<
td>Designation</td><td><input type="text" name="txt2"></td>
</tr>
<
tr>
<
td>Sal</td><td><input type="text" name="txt3"></td>
</tr>
<
tr>
<
td>Qualification</td><td><input type="text" name="txt4"></td>
</tr>
</
table>
</
center>
</
body>
</html>

Output

In the above code we created a table named employee. There are four fields in the employee table i.e. name, designation, sal, qualification.

image2.jpg

Insert Data in Table

Now we will write the code to insert data into the database table. The code for inserting data into the database table is as follows:

<?php
$con=mysql_connect ("localhost","root","");
mysql_select_db(
"mcn",$con);
@$a=$_POST[
'txt1'];
@$b=$_POST[
'txt2'];
@$c=$_POST[
'txt3'];
@$d=$_POST[
'txt4'];
if(@$_POST['inser'])
{
 $s=
"insert into employee values ('$a','$b','$c','$d')";
echo "Your Data Inserted";
 mysql_query ($s);
}
$con=mysql_connect (
"localhost","root","");
mysql_select_db (
"mcn",$con);
if(@$_POST ['sel'])
{
echo $ins=mysql_query ("select * from employee");
echo "<table bgcolor=skyblue border='2'>
   <tr>
   <th colspan=4>Select Data From Employee Table</th></tr>
   <tr>
   <th>Nmae</th>
   <th>Designation</th>
   <th>Sal</th>
   <th>Qualification</th>
   </tr>"
;
  
while ($row=mysql_fetch_array ($ins))
   {
  
echo "<tr>";
  
echo  "<th>".$row ['Name']."</th>";
  
echo  "<th>". $row ['Designation']. "</th>";
  
echo  "<th>". $row ['Sal']."</th>";
  
echo  "<th>".$row ['Qualification']."</th>";
  
echo "</tr>";
   }
   }
  
echo "</table>"
?>
<html>
<
head>
</
head>
<
body bgcolor="pink">
<table bgcolor="skyblue" border="2">
<form method="post">
<tr>
<
td colspan=2 align="center">Employee Table</td>
</tr>
<
td>Name</td><td><input type="text" name="txt1"></td>
</tr>
<
tr>
<
td>Designation</td><td><input type="text" name="txt2"></td>
</tr>
<
tr>
<
td>Sal</td><td><input type="text" name="txt3"></td>
</tr>
<
tr>
<
td>Qualification</td><td><input type="text" name="txt4"></td>
</tr>
<
tr>
<
td><input type="submit" name="inser" value="Insert"></td>
<td><input type="submit" name="sel" value="Select"></td>
</tr>
</
form>
</
table>
</
body>
</html

Output

Now we will insert the data into a HTML table then click on the insert button.

image3.jpg

When you click on the insert button then your data will be inserted in the database table. Like as in the following image.

image4.jpg

Select Data From Database

If you want to select all data from the database into the HTML table then you can do that. For this purpose you will be click on the select button. When you click on the select button then your data will be displayed in the HTML table, such as in the following image:

image5.jpg

Conclusion

So in this article you saw, how to insert and select data from a database into a HTML table. Using this article one can easily understand insert and select data from database in to HTML table.

Some Helpful Resources

Login to add your contents and source code to this article
post comment
     

Hi aril, there will be no error if there are an error then you will send your mailid, I will send code in your mail. Thanks.....

Posted by Vineet Kumar Saini Mar 13, 2012

it's has an error on this line "while ($row=mysql_fetch_array $ins))" it show Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\test\insert.php on line 28

Posted by aril fik Mar 10, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.