Home Tutorial Php Phpdatabase Displaying Database information on the browser.

 
 

Share on Google+Share on Google+
Displaying Database information on the browser.
Posted on: January 23, 2010 at 12:00 AM
Advertisement
Up to now, we learned how to create database and insert information in the database. In this tutorial, I will show you how to insert the information by using form on a website.

PHP DATABASE Information

Part-5(b): Displaying Data (with the help of PHPMyAdmin)

Up to now, we learned how to create database and insert information in the database. In this tutorial, I will show you how to insert the information by using form on a website.

First of all we will create a contact form in HTML. Let's see the following steps to displaying the data on the website :

1. First we will create the contact form called "contact.php" by using basic HTML code.

Page 1 : contact.php

<html>

<head>

<title>Contact Form</title>

</head>

<body>

<form action="display.php" method="post">

<table  align="left" cellpadding="4" cellspacing="2" border="0">           

<tr>

<td colspan="2"><h1>Please fill the following Information</h1></td>

</tr>

<tr>

<td>First Name :</td>

<td><input type="text" name="fname" value="" size="30" /></td>

</tr>

<tr>

<td>Last Name :</td>

<td><input type="text" name="lname" value="" size="30" /></td>

</tr>

<tr>

<td>Phone :</td>

<td><input type="text" name="phone" value=""  size="30" /></td>

</tr>

<tr>

<td>Mobile :</td>

<td><input type="text" name="mobile" value="" size="30" /></td>

</tr>

<tr>

<td>Fax :</td>

<td><input type="text" name="fax" value="" size="30" /></td>

</tr>

<tr>

<td>Email :</td>

<td><input type="text" name="email" value=""  size="30" /></td>

</tr>

<tr>

<td>Web :</td>

<td><input type="text" name="web" value=""  size="30" /></td>

</tr>

<tr>

<td>Address :</td>

<td><textarea name="addr" rows="5" cols="23"></textarea></td>

</tr>

<tr>

<td align="center" colspan="2"><input type="button" name="button" value="Submit" /></td>

</tr>

</table>

</form>

</body>

</html>

Your form will be displayed below :

You can edit the page according to your requirement, you can also add color, mentioned the width of the text boxes or anything whatever you need. I just show you the simple example.

2.The next step is to create the display.php file. This file will update the database with the new information. Create a new file called display.php

A. So, lets start the PHP coding with the open and close php tags.

<?php

?>

B. The second step is to hold the data sent from the contact.php and store the whole information into variable. Because we use form method to collect the data.

?php

$fname = $_POST['fname'];

$lname = $_POST['lname'];

$phone = $_POST['phone'];

$mobile = $_POST['mobile'];

$fax = $_POST['fax'];

$email = $_POST['email'];

$web = $_POST['web'];

$address = $_POST['addr'];

?>

C. The next step is to make a connection with the database.

<?php

$fname = $_POST['fname'];

$lname = $_POST['lname'];

$phone = $_POST['phone'];

$mobile = $_POST['mobile'];

$fax = $_POST['fax'];

$email = $_POST['email'];

$web = $_POST['web'];

$address = $_POST['addr'];

 

mysql_connect("localhost","root","") or die ("Error : " . mysql_error());

mysql_select_db("contact_roseindia");

?>

In the above code, I connect the form with the database and also replace the ?localhost? with the location of my server, ?root? with my username, ?? with my password.  You can also replace the setting of your computer. Then, I select the database that we created in the last tutorial that is "contact_roseindia".

D. The next step is to create the query that will insert a new row in the database. As you can notice we are inserting date in the table called tbl_contact and  are inserting the fields ID, FName, LName, Phone, Mobile, Fax, Email, Web, and Address.

We left the ID field Null because it is auto generated by the database but the actual values are coming from the variables.

<?php

$firstname = $_POST['fname'];

$lastname = $_POST['lname'];

$phone = $_POST['phone'];

$mobile = $_POST['mobile'];

$fax = $_POST['fax'];

$email = $_POST['email'];

$web = $_POST['web'];

$address = $_POST['addr'];

mysql_connect("localhost","root","") or die ("Error : " . mysql_error());

mysql_select_db("contact_roseindia");

$query = "INSERT INTO tbl_contact(ID,FName,LName,Phone,Mobile,Fax,Email,Web,Address) VALUES

('NULL','.$firstname','.$lastname','.$phone','.$mobile','.$fax','.$email','.$web','.$address')";

?>

E. Now what we need to do is to execute the query. If it is successful we will display the information that is inputted to the database and if  the query fails we will print an error.

<?php

$firstname = $_POST['fname'];

$lastname = $_POST['lname'];

$phone = $_POST['phone'];

$mobile = $_POST['mobile'];

$fax = $_POST['fax'];

$email = $_POST['email'];

$web = $_POST['web'];

$address = $_POST['addr'];

mysql_connect("localhost","root","") or die ("Error : " . mysql_error());

mysql_select_db("contact_roseindia");

$query = "INSERT INTO tbl_contact(ID,FName,LName,Phone,Mobile,Fax,Email,Web,Address) VALUES

('NULL','$firstname',.$lastname',.$phone','$mobile','$fax','$email','$web','$address')";

mysql_query($query) or die ("Error Updating Database");

echo "The Inserted Value is: " .$firstname." ".$lastname." ".$phone." ".$mobile." ".$fax." ".$email." ".$web." ".$address;

?>

Contact_form1_image

When you submit the contact form, it will show the certain output :

The Inserted Value is: Satya Prakash 011-25156545 9810232323 011-25844242 satyapra02@gmail.com www.sp02.com Sector-3, Rohini, New Delhi.

Advertisement

Related Tags for Displaying Database information on the browser.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 23, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com