Insert data into mysql

This tutorial will show you how to insert data into mysql database.


"INSERT INTO table_name(column_name1, column_name2)VALUES('value1, 'value2')" ;


In this tutorial, create 2 files
1. insert.php
2. insert_ac.php

1. Create table "test_mysql" in database "test".
2. Create file insert.php.
3. Create file insert_ac.php.

If you don't know how to create database and table click here to learn

STEP1: Create table "test_mysql"

CREATE TABLE `test_mysql` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',

STEP2: Create file insert.php

View In Browser

############### Code

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<td><form name="form1" method="post" action="insert_ac.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>
<td width="71">Name</td>
<td width="6">:</td>
<td width="301"><input name="name" type="text" id="name"></td>
<td><input name="lastname" type="text" id="lastname"></td>
<td><input name="email" type="text" id="email"></td>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>

STEP3: Create file insert_ac.php


############### Code


// Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form

// Insert data into mysql

$sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname', '$email')";

// if successfully insert data into database, displays message "Successful".
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";

else {
echo "ERROR";

// close connection

Similar Post
Update data in mysql

Update data in mysql

Update/Edit data from mysql database, can do it easily.
Select data From mysql

Select data From mysql

When you need data from your mysql database to show in your web page, you need to select database, table and what's row you want to pull its data first.