How to display MySQL data in HTML table using Php


The most attractive and best way to display the database records in table format. Below is the simple example to display the records stored in MySQL table.
Record Table of Employee
NAME
DEVICE_NAME
NUMBER_OF_UNITS SOLD 
TOTAL_PRICE
ROHAN
OVEN 
10
70300
SHAYAM
LAPTOP
8
305000
KAMRAN
CAMERA 
15
10200
NATHAN
CELL PHONE 
20
80000
MARAYA
TELEVISION
5
60000
The above table is a sample example of MySQL table with some records of data.

We need to fetch these data in HTML table to view the data on browser.

<?php
@mysql_connect('localhost','root','') or die('check username and password');
@mysql_select_db('record') or die('not connected');
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
<style type="text/css">
table
{
 width:50em;
}

fieldset
{
 width:20em;
 background-color:lightgreen;
}
</style>
</head>
<body bgcolor="lavender">
<div>
<center>
<fieldset>
<legend><font size="5px" color="blue">Record of Employee</font></legend>
<table border="1" rules="rows">
<tr bgcolor="red"><th>NAME</th><th>DEVICE NAME</th><th>NUMBER OF UNITS</th><th>TOTAL AMOUNT</th></tr>
<?php
$query = mysql_query("select * from employee");
while($result = mysql_fetch_array($query))

?>
<tr align="center"><td><?php echo $result['name']; ?> </td><td><?php echo $result['device_name']; ?> </td><td><?php echo $result['number_of_units']; ?> </td><td><?php echo $result['total_price']; ?> </td></tr>
<?php } ?>
</table> 
</fieldset>
</center>
</div>
</body>
</html>

Output of the above



How to Insert data in Database using Php


Data can be entered into MySQL table by executing SQL INSERT statement with Php mysql_query( ) function. Before this make sure you have created database and table in PhpMyadmin. Below is very simple example to enter data in MySQL using HTML form. Because in live project value are take from HTML form and captured using Php script and this captured value finally inserted in MySQL Table .

Example :-

Creating database and table :-
Open PhpMyadmin using username as root and keeping password field blank. Now click on new located on left side. After clicking on window will open asking for database name. Provide the database name as record and click OK. Now new window will open asking table name and number or columns. provide the table name as employee and give 8 numbers of columns and click OK.
Name
Contact
email
Password
Gender
Language
Nationality
Address









































Making connectin with PhpMyadmin

connection.php
<?php
@mysql_connect('localhost','root',' ') or die('check username and password');
@mysql_select_db('record') or die('not connected');
?>

HTML Code (insert.php)
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
<style type="text/css">
table
{
width:50em;
}
table td
{
border:none;
}
fieldset
{
width:20em;
background-color:lightgreen;
}
</style>
</head>
<body bgcolor="lavender">
<div>
<form action="data.php" method="post">
<center />
<fieldset>
<legend><font size="5px" color="blue">Insert data in SQL TABLE</font></legend>
<table border="1" cellspacing="10" cellpadding="10">
<tr><td>Name :- </td><td><input type="text" name="name"></td></tr>
<tr><td>Contact number :- </td><td><input type="number" name="contact"></td></tr>
<tr><td>Email :- </td><td><input type="text" name="email"></td></tr>
<tr><td>Password :- </td><td><input type="password" name="password"> </td></tr>
<tr><td>Gender :- </td><td><input type="radio" name="gender">Male <input type="radio" name="gender">female</td></tr>
<tr><td>Language Known :- </td><td><input type="checkbox" name="list[]" value="Hindi">Hindi
<input type="checkbox" name="list[]" value="English">English
<input type="checkbox" name="list[]" value="Marathi">Marathi
<input type="checkbox" name="list[]" value="Punjabi">Punjabi
<input type="checkbox" name="list[]" value="Tamil">Tamil
</td></tr>
<tr><td>Nationality :- </td><td><select name="nation">
<option value="-- Select Here --"> -- Select Here -- </option>
<option value="India"> India </option>
<option value="China"> China </option>
<option value="Japan"> Japan </option>
<option value="Australia"> Australia </option>
<option value="Sri Lanka"> Sri Lanka </option>
</select>
</td></tr>
<tr><td>Address :- </td><td><textarea rows="5" cols="30" name="address"></textarea></td></tr>
<tr align="center"><td colspan="2"><input type="submit" name="submit" value="submit"></td></tr>
</fieldset>
</form>
</div>
</body>
</html>
Save this as insert.php

PHP Code(data.php)
<?php
include_once('connection.php');

if($_SERVER["REQUEST_METHOD"] == "POST")   //  or  if(isset($_POST['submit']))
{
$name = check($_POST['name']);
$email = strtolower(check($_POST['email']));
$contact = check($_POST['contact']);
$password = check($_POST['password']);
@$gender = $_POST['gender'];
@$list = implode(",",$_POST['list']);
$nation = $_POST['nation'];
$address = check($_POST['address']);

if(empty($name))
{
$msg = "Please enter your name";
}
else if(empty($contact))
{
$msg = "please enter valid contact number ";
}
else if(strlen($contact) !== 10)
{
$msg = "please enter valid contact number";
}
else if(empty($email))
{
$msg = "Please enter your email";
}
else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$msg = "please enter valid email address";
}
else if(empty($password))
{
$msg = "Please enter your password";
}
else if(strlen($password) <6)
{
$msg = "pssword must not be less than six characters";
}
else if(strlen($password) >15)
{
$msg = "pssword must not be more than fifteen characters";
}
else if(empty($gender))
{
$msg = "Please select your gender";
}
else if(empty($list))
{
$msg = "Please select the language known";
}
else if($nation === '-- Select Here --')
{
$msg = "please select your nationality";
}
else if(empty($address))
{
$msg = "please provide your communicating address";
}
else 
{
if(mysql_query("insert into employee(name,contact,email,password,gender,language,nationality,address) values('$name','$contact','$email','$password','$gender','$list','$nation','$address')"))
{
echo "<center><font size=7px color=blue>SUCCESS</font> <br> <a href=test.php><button>OK</button></a></center>";
}
else
{
echo "<center><font size=7px color=blue>Server Error !!!!</font> <br> <a href=test.php><button>OK</button></a></center>";
}
}
}
?>
<?php
function check($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
if(isset($msg))
{
echo "<center><font size=7px color=blue>  $msg  </font> <br> <a href=test.php><button>OK</button></a></center>";
}
?>
Save this as data.php

Description :-
For Php code
Include_once( ) function is used to inherit the code of connection.php.
Strtolower( ) function is used to convert the string into lower case.
Implode( ) function is used to insert the comma between the string value.
Empty( ) function is used to check whether the input field is empty or note.
Mysql_query( ) function is used to execute the SQL query.
The function check( ) is used to restrict the user to perform anti programming.