Insert Database MySQL with PHP form.

INSERT DATABASE MYSQL WITH PHP FORM

After installed PHP , MySQL and phpmyadmin now you must try how to insert database
through PHP as API. It's a basic for web programmer to create form for user login , sign up, make comments and other user's activity on your website.

First create php form using HTML form assume it named contact_form.php and put it into htdocs folder on apache server.
Note : I assume you had understood about HTML5 and  CSS/CSS3.



Then open you browser and then type 127.0.0.1/contact_form.php and it will shown like this :

Create connect_db.php file to connect between PHP and MySQL :





After create the form now you should make the form work, then create contact-form-save.php.


Before you fill and submit your form, open your phpmyadmin and then see your previous table data on table my_table_1

Now we will fill it through PHP form :



And if you successful to insert your data into database the result will be like this :


If you open your phpmyadmin there is new record on your table :


CREATE SIMPLE FORM VALIDATION

To avoid invalid data come into our database, we need to make filter / form validation,
back to your code (contact-form-save.php) and then use this code :



Now open contact_form.php (127.0.0.1/contact_form.php) leave it all inputs blank and then click submit here the result:


MAKE DYNAMIC FORM

Usually user won't spent their time to back into page form to make correction on incorrect form, so you need to make dynamic form so that when there is error user doesn't need to back into page form.

First back into contact_form.php and then add these codes on each label like these :



And on contact-form-save.php update your code like these :



- include : to put another file in current page so that can be use / displayed other file on current page.
- global : sign / key to make your variable can be use to entire the part of file.
- $_POST : to take value from input according on name.

other documentation you can see on : https://www.w3schools.com/php/

Now, if you leave entire columns blank or one of it blank it will result :


or like this :


Next article I will show you how to display data from database, update and delete.

Comments

Buy me a coffeeBuy me a coffee