Displaying Database using PHP


 

Displaying Database using PHP

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.

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>

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

</tr>

7

</table>

</form>

</body>

8

</html>

Your form will look like this :

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

0

Let's see the code to understand it properly.

<?php

$username = "root";

1

$password = "";

$hostname = "localhost";

$database = "contact_rose";

2

$firstname = $_POST['fname'];

$lastname = $_POST['lname'];

$phone = $_POST['phone'];

3

$mobile = $_POST['mobile'];

$fax = $_POST['fax'];

$email = $_POST['email'];

4

$web = $_POST['web'];

$address = $_POST['addr'];

$databasecreation="CREATE DATABASE ".$database;

5

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

mysql_query($databasecreation);

mysql_select_db($database,$conn);

6

$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')";

7

mysql_query($insert);

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

echo "<table border='1'>

8

<tr>

<th>ID</th>

<th>First Name</th>

9

<th>Last Name</th>

<th>Phone</th>

<th>Mobile</th>

0

<th>Fax</th>

<th>Email</th>

<th>Web</th>

1

<th>Address</th>

</tr>";

while($row = mysql_fetch_array($result))

2

{

echo "<tr>";

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

3

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

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

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

4

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

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

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

5

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

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

echo "</tr>";

6

}

echo "</table>";

?>

7

Your form will shown like below.

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

8

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

Ads