PHP Mysqli Insert Data into Database
In this tutorial you will learn about how to insert data into mysql database using PHP. MySQLi is the enhanced version of Mysql.
The INSERT INTO statement is used to insert new rows into database table. Before accessing MySQL database, first we need to connect with database using Mysqli_connect() PHP function.
MySQL Database Table Structure
PHP insert data into mysq database |
How to create an html form that stores data in a mysql database using php
HTML FORM to Insert New Record into Table
Create a file insert.php and create HTML form with four input fields and a submit button. HTML form method is post to insert data into database using PHP. Here I am using Bulma css framework to design HTML form.
First of all create four input fields for first name, last name, email and age. Then create a button with type submit. Button should be within html form otherwise button will not submit input fields text to PHP insert data script.
Insert Data into Database Using PHP
html form to insert data into database using php |
First of all create four input fields for first name, last name, email and age. Then create a button with type submit. Button should be within html form otherwise button will not submit input fields text to PHP insert data script.
When a user clicks on submit button of html form, the form data is sent to PHP script. The PHP script first connect with database and retrieves the values from the html form with the PHP $_POST[] variables.
Then the PHP mysqli_query() function execute the INSERT INTO statement and a new records will insert into database table person.
PHP Script to Insert Data into Table
You can write this PHP script within another file and then include that file within HTML form insert.php file or write this script above the HTML starting tag. First of all create an if statement in php script.
Within this if statement create a connection to the MySQL database and store this mysqli_connect() function into a variable conn. Now we will check if any error in connection with database an error will display and program terminate.
Next assign input fields to variables like $fname=mysqli_real_escape_string($conn,$_POST('txtfname')). Here we will use mysqli_real_escape_string() PHP function for protection against sql injection .
Now create a variable sql and assign it SQL insert query and run the this query using mysqli_query() function to insert data into table.
php code for inserting data into database from.
After following above steps you will be able to create a person table, an html form and a php script to insert data into mysql database using php. If you have any question then comment below to get answer of your question. Follow this blog to get more articles reltaed PHP,HTML,CSS,Database,Jquery,css framework and about how to create and customize blog.
Download Source Code
No comments:
Post a Comment