To insert records in a database table, required these steps
- First of all create the database if you want to use new database.
- create tables if u want to use new table in which values will be inserted.
- create connection file.
- 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.- Values are recieved by using $_GET or $_POST
- Write SQL Query.
- Execute the Query.
Please see the images to clearify the data insertion concept and then check explaination of all this process.
insert-records.phpIn 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:
Post a Comment