How to display MySQL data in Text box using Php


To display MySql data in Textbox we need to use Php script in HTML script. below is the simple example to display data in Text Box ....
In this example you will see Text Box displaying data directly from the database
Suppose this is the MySql database containing some data
NAME
CONTACT
EMAIL
ADDRESS
MUKESH SINGH
9475563334
Dhanbad Jharkhand
PRASHANT PASHWAN
9475551446
Madhapura Bihar
SACHIN
8986822639
Parasnath Jaharkhand

Php code and Html code
<?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;
}
.sty
{
width:16em;
height:4em;
}
</style>
</head>
<body bgcolor="lavender">
<div>
<center>
<fieldset>
<legend><font size="5px" color="blue"> Fetching of data from Sql to Textbox </font></legend>
<?php
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$address = $_POST['address'];
$contact = $_POST['contact'];

if(empty($email))
{
echo "<font color=red size=4px> please enter email address </font>";
}
else
{
$query = mysql_query("select * from employee where email='$email'");
$result = mysql_fetch_array($query);
$n = $result['name'];
$a = $result['address'];
$c = $result['contact'];
}
}
?>
<form method="post" action="">
<table border="1" cellspacing="10" cellpadding="10" rules="rows">
<tr><td colspan="2">enter your email address &nbsp &nbsp &nbsp <input type="email" name="text"> <input type="submit" name="submit" value="submit"></td></tr>
<tr><td> Name </td><td><input type="text" readonly name="name" value="<?php if(isset($n))
{
echo $n;
}
?> "></td></tr>
<tr><td> Address </td><td> <input class="sty" type="text" name="address" readonly value="<?php if(isset($a))
{
echo $a;
}
?> "></td></tr>
<tr><td> Contact No. </td><td><input type="text" readonly name="contact" value="<?php if(isset($c))
{
echo $c;
}
?> "></td></tr>
</table>
</fieldset>
</center>
</div>
</body>
</html>

How to display MySQL data in Drop Down List using Php


To display MySQL table data in Drop Down List we need to mix the Php script with HTML. See  the below example how to perform this.

Suppose this is the SQL table with some data
State_Name
State_Code
Jammu and Kashmir
01
Punjab
02
Gujarat
03
Madhya  Pradesh
04
Mumbai
05
Goa
06
Jharkhand
07
Orissa
08
Chennai
09
Andhra Pradesh
10
Jharkhand
11
Bihar
12
West Bengal
13

Now i will show the state name in Drop Down List

<?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"></font></legend>
<select name="state_name">
<option>--Select your State--</option>
<?php
$query = mysql_query("select name from employee");
while($result = mysql_fetch_array($query))

?>
<option><?php echo $result['state_name']; ?> </option>
<?php } ?>
</select>
</fieldset>
</center>
</div>
</body>
</html>

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.


How to convert string in Uppercase in Php


To convert string in uppercase Php has inbuilt function strtoupper( ) using this function one can convert the lowercase string into uppercase.
<?php
if(isset($_POST['submit']))
{
$name = strtoupper($_POST['name']);

if(empty($name))
{
$msg = "Please enter your full name";
}
else
{
$msg =  "Your name is :- " . $name;
}
}
?> <!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body bgcolor="thistle">
<fieldset>
<form method="post" action="">
Enter your full name :- <input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($msg))
{
echo "<script>alert('$msg')</script>";
echo  "<font color=yellow size=6px>" . $msg . "</font>";
}
?>
</fieldset>
</body>
</html>



How to convert string in Lowercase in Php


To convert string in lowercase Php has inbuilt function strtolower( )
<?php
if(isset($_POST['submit']))
{
$name = strtolower($_POST['name']);

if(empty($name))
{
$msg = "Please enter your full name";
}
else
{
$msg =  "Your name is :- " . $name;
}
}
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body bgcolor="thistle">
<fieldset>
<form method="post" action="">
Enter your full name :- <input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($msg))
{
echo "<script>alert('$msg')</script>";
echo  "<font color=yellow size=6px>" . $msg . "</font>";
}
?>
</fieldset>
</body>
</html>