Home Tutorial Php Phpdatabase Displaying Database using PHP

 
 

Share on Google+Share on Google+
Displaying Database using PHP
Posted on: January 23, 2010 at 12:00 AM
Advertisement
Till now, we have done the creation part of database and table. In this tutorial, I will show you how to insert the information by using form on a website with the help of PHP Script.

PHP DATABASE

Part-5(d) : Displaying Data (with the help of PHP Scripts)

Till now, we have done the creation part of database and table. In this tutorial, I will show you how to insert the information by using form on a website with the help of PHP Script.

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 same as we have done in Part 5(b).

Page : contact.php

<html>

<head>

<title>Contact Form</title>

</head>

<body>

<form action="create_database.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="submit" name="button" value="Submit" /></td>

</tr>

</table>

</form>

</body>

</html>

Your form will look like this :

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 second step is to assign the values of the form in the variable and insert into the database and this will be done in page create_database.php.

There will be a quite small change in the code at the time of insert the value.

Let's see the code to understand it properly.

<?php

$username = "root";

$password = "";

$hostname = "localhost";

$database = "contact_rose";

$firstname = $_POST['fname'];

$lastname = $_POST['lname'];

$phone = $_POST['phone'];

$mobile = $_POST['mobile'];

$fax = $_POST['fax'];

$email = $_POST['email'];

$web = $_POST['web'];

$address = $_POST['addr'];

$databasecreation="CREATE DATABASE ".$database;

$conn = mysql_connect($hostname, $username, $password)or die("Unable to connect to MySQL");

mysql_query($databasecreation);

mysql_select_db($database,$conn);

$query="CREATE TABLE contacts(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, First_Name varchar(30) NOT NULL, Last_Name varchar(30) NOT NULL, Phone varchar(15) NOT NULL, Mobile varchar(15) NOT NULL, Fax varchar(15) NOT NULL, Email varchar(30) NOT NULL, Web varchar(50) NOT NULL, Address varchar(255) NOT NULL)";

mysql_query($query);

$insert = "INSERT INTO contacts VALUES('','$firstname','$lastname','$phone','$mobile','$fax','$email','$web','$address')";

mysql_query($insert);

$result = mysql_query("select * from contacts");

echo "<table border='1'>

<tr>

<th>ID</th>

<th>First Name</th>

<th>Last Name</th>

<th>Phone</th>

<th>Mobile</th>

<th>Fax</th>

<th>Email</th>

<th>Web</th>

<th>Address</th>

</tr>";

while($row = mysql_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['ID'] . "</td>";

echo "<td>" . $row['First_Name'] . "</td>";

echo "<td>" . $row['Last_Name'] . "</td>";

echo "<td>" . $row['Phone'] . "</td>";

echo "<td>" . $row['Mobile'] . "</td>";

echo "<td>" . $row['Fax'] . "</td>";

echo "<td>" . $row['Email'] . "</td>";

echo "<td>" . $row['Web'] . "</td>";

echo "<td>" . $row['Address'] . "</td>";

echo "</tr>";

}

echo "</table>";

?>

Your form will shown like below.

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 using PHP:


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