Kamis, 15 Maret 2012

Saving Data to the Database and Displaying Data with PHP

In the tutorial Saving Data to Database & Data with PHP to showing this will be discussed how to build a connection to a MySQL database, saving data to a database, read data from database and display the data to the user. This tutorial requires basic knowledge of MySQL you. If you did not already have a basic knowledge of MySQL please visit the website to learn MySQL www.MySQL.com first. Because in this tutorial will not be discussed in depth about MySQL.

Saving Data to Database
Create a database with the name db_contact_us, then create a table named tb_contact_us, with the following structure: 
Field Tipe Data
Name Varchar(50)
Email Varchar(50)
Comment Text 

Create a Contact Form
Make a new HTML document with your text editor and create the HTML form as follows:


<html>
<head>
<title>Contact US</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="save_contact.php">
<p>Name :
<input name="name" type="text" id="name" />
</p>
<p>
Email :
<input name="email" type="text" id="email" />
</p>
<p>Comment :
<textarea name="comment" id="
comment"></textarea>
</p>
<p>
<input type="submit" name="Submit" value="
comment" />
</p>
</form>
</body>
</html>


Save the HTML document with the name of the directory contact_form.html and put it on your web server.

Make PHP Script
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<?php
//connect to the database
$conn=mysql_connect("localhost", "root", "root");
mysql_select_db("db_contact_us");

//create a variable for which data is transmitted
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$comment=$_REQUEST['comment'];
//create query to storing data
$sql="INSERT INTO tb_contact_us (name, email, comment) values ('$name','$email','$comment')";
//saving to the database
mysql_query($sql);
echo "<h2>Data has been saved</h2>";
?>
</body>
</html> 

 
Now save with the name of the directory save_contact.php and put it on your web server. Then see the results on your web browser. If you have done it correctly then it will be as follows:


Displaying data 
After you can save data to the database now you will learning how to read data from database and display it to the user. To read data from a database you must first connect to the database, and then read the data using the function mysql_query () and mysql_fetch_array (). To more clearly do the following example.

Create a new document with your text editor, and begin to make the initial HTML document.

<html>
<head>
<title>Display Data</title>
</head>
<body>
<?php
//create connection to the database
$conn=mysql_connect("localhost", "root", "root");
mysql_select_db("db_contact_us");
//create query select
$sql="select * from tb_contact_us";
//reading data
$hasil=mysql_query($sql);
//display data
while($row=mysql_fetch_array($hasil)){
//display field name
echo "Name : ".$row['Name'];
//display field email
echo "<br>Email : ".$row['Email'];
//Display field Comment
echo "<br>Comment : ".$row['Comment'];
echo "<hr>";
}
?>
</body>
</html> 

Now save with the name of the directory read_data.php and put it on your web server. Then see the results on your web browser. If you have done it correctly then it will be as follows

Good Luck Guys :-)

Tidak ada komentar:

Posting Komentar