Insertion of records in database using php and mysql

In this tuorial we will learn how to insert records in mysql database.


To insert records in a database table, required these steps


  1. First of all create the database if you want to use new database.
  2. create tables if u want to use new table in which values will be inserted.
  3. create connection file.
  4. create a php file that contains a form using this form values will be inserted to the database.

Process of Inserting Records in a database table

Values goes to the next page, the page defined in the action attribute, after the form is submitted.

  1. Values are recieved by using $_GET or $_POST
  2. Write SQL Query.
  3. Execute the Query.

Please see the images to clearify the data insertion concept and then check explaination of all this process.

insert-records.php

In this php file we creates a form that contains two fields one is Name and other is FatherName.

To get Name, we have textbox and the name attribute of textbox is name. I think you are confusing???

Ok Listen, the name attribute of the field is the really big boss who stores the value whatever user writes in the textbox, for the Name of user, i place a textbox and give its name attribute a value called name, now this textbox will be accessed in the next page, whenever form is submitted by its name attribute, in this case its name=name.

Same for Father Name, we have a textbox whose name attribute is fname and this fname make it possible to access the value of this textbox in the next page.

action="do-insert.php", this is the page where form elements will be accessed and insertion will placed.

method=post means that form values will be transfered using post method.



do-insert-records.php

In this php file, first of all i include the connection file, then form elements that are coming from previous page are accessed and stored in the variables.

Then i write the sql query and save it in a variable called $queryinsert. and for executing this query, i passed $queryinsert as a parameter and execute the query.

Note:- mysql_query() is used to execute the mysql query.

After executing the insertion query i save the resultset in a variable $resultset and then check whether resultset is true or not, if resultset is true then its mean our query has been successfully executed, else we are going to print the error to find out why query is not executed.



conn.php



Output

0 comments: