News

PHP Tutorial 9: PHP insert statement

php insert

This tutorial will guide you on how to insert data into database table through webpage using a PHP script. Storing data into the database is much needed by any website. This is much simple and easier to accomplish using PHP. Inserting a data into the database table is done using INSERT INTO statement.

The MySQL command to insert values into a specific table is

INSERT INTO TABLE_NAME VALUES (VALUE1,VALUE2,VALUE3);

First of all you need to create a table in MySQL database using CREATE TABLE statement. Below is a example that shows you how to create a table. I am going to create a table with two records.

CREATE TABLE EMP(NAME VARCHAR(20), JOB VARCHAR(20));

Then all you need is to copy and paste the script given below as save it as any name you want. Also things to be changed are the username, server_name, password and database name in the following command.  Do it according to your database you have.

$connected=mysql_connect('localhost','root','')
mysql_select_db('dba',$connected);

The complete script is as :

<html>

<body>
<h1> Fill the form please </h1>
<hr>

<FORM METHOD="POST" ACTION="insert.php">
<TABLE>
  <TR>
    <TD>Name</TD>
    <TD>
      <INPUT TYPE="TEXT" NAME="name" SIZE="20">
    </TD>
  </TR>
  <TR>
    <TD>Job</TD>
    <TD><INPUT TYPE="TEXT" NAME="job" SIZE="25"></TD>
  </TR>
</TABLE>
<P><INPUT TYPE="SUBMIT" VALUE="Submit" NAME="submit"></P>
</FORM>

<?php
session_start();
{
if (isset($_POST['submit']))
{

$var1 = $_POST['name'];
$var2 = $_POST['job'];

if(!empty($var1))

{

$connected=mysql_connect('localhost','root','')
or die("Cannot connect to the database");

mysql_select_db('dba',$connected);

mysql_query("INSERT INTO emp (name,job) VALUES ('$var1', '$var2')");

echo "Your name is :\t".$var1."<br>";
echo "Your job is :".$var2;

header( "refresh:5; url=insert.php" );

}
else echo "PLEASE ENTER THE NAME";

}

else
{
echo "PLEASE ENTER THE VALUES";
}
}

?>

</body>
</html>

This is how output of the script looks.

Let me show you what is in the database.

The key operations that I have designed in this script are :

1.)  The user is not redirected to another page to show confirmation.

2.) The values in the form fields get erased automatically.

3.) No empty fields are inserted into the database. That is you cant insert empty name.

4.) After 5 seconds the page is refreshed and user can see another text being displayed.

5.) Duplicate values are not inserted into the database for every page refresh.

This is very much optimized and simplified script for beginners. You can customize the script any way you want. Add new fields, check boxes and radio buttons to gather more information from the user. Feel free to use it.

Make sure you bookmark our website or subscribe to our RSS feeds to get all the updates from us.

3 Comments

3 Comments

  1. vikram

    October 14, 2011 at 3:12 PM

    thank you so much that was really helpfull 🙂

  2. vikram

    October 14, 2011 at 2:59 PM

    How can i use the script is a complex form. Form where variables has to be deleted after the use. Here i see objects are stored at for long time

    • Khan Azharuddin12345

      October 14, 2011 at 3:11 PM

      yes you can use this script for complex forms as well.. Use sessions to delete the variables after they are used. Generate session ID’s and compare them every time user refreshes the page. Also use function .Try to keep all the logic in functions and all them once.. like this:

      session_start();
      if (isset($_POST[‘submit’]))
      {
      insertform();
      }
      else{
      displayform();
      }

      i hope u got it…. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top