PHP simple : PHP tutorial, MySQL tutorial
PHP Tutorial - Free PHP Script

Home | Tutorials | Php vs Node.Js | Web Tools

MySQL : Insert Record


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

<!-- set this form to POST method and target this form to insert.php -->
<form id="form1" name="form1" method="post" action="insert.php">
<p>Name :
<!-- name of this text field is "name" -->
<input name="name" type="text" id="name" />
<br />
Email :
<!-- name of this text field is "email" -->
<input name="email" type="text" id="email" />
<br />
Tel :
<!-- name of this text field is "tel" -->
<input name="tel" type="text" id="tel" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>

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

Advertise
Put Ebay RSS Feeds onto your website
SEO Elite Software
Domain Dashboard CPanel & Seo Manager
Work at Home Ideas and Opportunities
Introduction & Get Start
Using Form
PHP
PHP Tags
PHP Variables
PHP Control Structures
MySQL
Create Database & Table
Database Connection
Insert Record
Select Record
Update Record
Delete Record

PHPsimple.net
info@phpsimple.net