Thursday 3 March 2016

How to process forms using PHP & MySQL

What to Do:

Lets say Mr. Kopa is a university student. He want to take a course in the upcoming semester. So, he go to university website and click in the registration button.
Then registration form appear, he fill all of the field and hit submit button. He wanted to review his info by clicking review button.
Now, what happens during this process, consider the following picture.
 
Form Process






















> A html form is first showed up 
> When submit button is clicked data is processed by php script and stored to database.
> When review button is clicked php script retrieved from database, process it and  displayed.

Lets build our project:
We will distribute our workload to our stuff. 

  • index.php file will display form with the help of html
  • process-student.php file will take the info from index.php
  • process-student.php file will save the info in database by config.php
  • then courseform.php will show up and take info just line index.php
  • the view.php will display all info that already stored in database

What's inside them:

index.php file function:


html has two main part: head & body. In side body we will use <form> tab, post method will pass the data to action php script, in our case its process-student.php
<input> tab is used to make field and every field has a unique name. This unique name is used to pass data by post method.

config.php file function:


This file will provide the host-name, user-name, user-password , database-name of MySQL database.

process-student.php file function:

This file first load the config.php file to connect the database. '$_post' method will catch the data from index file by these unique name.
SQL query store data into the database table that we already created.
If SQL query somehow fail it will stop the script and show the specific error, or it will redirect to courseform.php file.

courseform.php file function:

This file function is as same as index file. It will take the course info from user.

process-course.php file function:

Same function as process-student file.


view.php file function:

This file log in the database and retrieve data from data-table by SQL query then displayed to the user.

That's the basic functions of our files.

How to run it: 
  • Download the source files from download link and place it to localhost root.
  • Create database as student_db and import the backup sql file.
  • Then go to browser and put the url: localhost/index.php


Happy Coding :)
@D