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.
In this file, there are including with simple form format. Set the form method to "POST" and the target file is "insert.php".
* 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.
* 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.
This file is the parameters receiver and save them to database. MySQL syntax for save parameters to database is below.
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
// Or you can do it in one line.
mysql_query(insert into table_name(column1, column2, ...) values('value1', 'value2', ...)");
// Connect database.
// Get values from form.
// 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
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".