Rabu, 07 Januari 2009

Cara insert record pada my sql

his tutorial will show you how to insert data into mysql database. For this tutorial you need two files. The first one is the simple HTML file and make it with a form. When you put the values and Submit to the other one "insert.php", this PHP file should be insert the values to the database.

1. Create a simple .html file including with form and save it to "form.html"
2. Create a .php file to recieve the values from "form.html". This file is "insert.php".
* Use database "test" and table "phonebook" from Create Database & Table tutorial.


• form.html

In this file, there are including with simple form format. Set the form method to "POST" and the target file is "insert.php".
form.html
* Form layout.

Name : <= name of this text box is "name"
Email : <= name of this text box is "email"
Tel : <= name of this text box is "tel"

* Form tags in this file.



Name :




Email :




Tel :








* Important : All your text fields should be including with names. There are important when you submit from "form.html", the target file "insert.php" should be know the parameters by these.


• insert.php

This file is the parameters receiver and save them to database. MySQL syntax for save parameters to database is below.

Syntax :

insert into table_name(column1, column2, ...) values('value1', 'value2', ...)

* For select, insert, update and delete commands you need to use mysql_query command together as these;

// Put insert command in $sql
$sql="insert into table_name(column1, column2, ...) values('value1', 'value2', ...)";

// Query it with mysql_query
mysql_query($sql);

// Or you can do it in one line.
mysql_query(insert into table_name(column1, column2, ...) values('value1', 'value2', ...)");

insert.php
// Connect database.
include("connectdb.php");

// Get values from form.
$name=$_POST['name'];
$email=$_POST['email'];
$tel=$_POST['tel'];

// Insert all parameters into database.
// The id field is auto increment. You don't have to insert any value
mysql_query("insert into phonebook(name, email, tel) values('$name', '$email', '$tel')");

// Close database connection
mysql_close();

?>

Test

Open form.html on your web browser as http://localhost/form.html. Put the values and click "Submit" button. When URL has change to http://localhost/insert.php, open a new browser and go to http://localhost/phpmyadmin (if you doesn't installed it Click Here how to install phpMyAdmin). Select database "test" and browse record on table "phonebook".

Tidak ada komentar:

Poskan Komentar