Displaying Database information on the browser.


 

Displaying Database information on the browser.

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.

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>

0

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

</tr>

<tr>

1

<td>Fax :</td>

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

</tr>

2

<tr>

<td>Email :</td>

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

3

</tr>

<tr>

<td>Web :</td>

4

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

</tr>

<tr>

5

<td>Address :</td>

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

</tr>

6

<tr>

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

</tr>

7

</table>

</form>

</body>

8

</html>

Your form will be displayed below :

9

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.

0

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

1

?php

$fname = $_POST['fname'];

$lname = $_POST['lname'];

2

$phone = $_POST['phone'];

$mobile = $_POST['mobile'];

$fax = $_POST['fax'];

3

$email = $_POST['email'];

$web = $_POST['web'];

$address = $_POST['addr'];

4

?>

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

<?php

5

$fname = $_POST['fname'];

$lname = $_POST['lname'];

$phone = $_POST['phone'];

6

$mobile = $_POST['mobile'];

$fax = $_POST['fax'];

$email = $_POST['email'];

7

$web = $_POST['web'];

$address = $_POST['addr'];

 

8

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

mysql_select_db("contact_roseindia");

?>

9

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.

0

<?php

$firstname = $_POST['fname'];

$lastname = $_POST['lname'];

1

$phone = $_POST['phone'];

$mobile = $_POST['mobile'];

$fax = $_POST['fax'];

2

$email = $_POST['email'];

$web = $_POST['web'];

$address = $_POST['addr'];

3

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

4

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

5

<?php

$firstname = $_POST['fname'];

$lastname = $_POST['lname'];

6

$phone = $_POST['phone'];

$mobile = $_POST['mobile'];

$fax = $_POST['fax'];

7

$email = $_POST['email'];

$web = $_POST['web'];

$address = $_POST['addr'];

8

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

9

('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;

0

?>

Contact_form1_image

1

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

The Inserted Value is: Satya Prakash 011-25156545 9810232323 011-25844242 [email protected] www.sp02.com Sector-3, Rohini, New Delhi.

Ads