Learn PHP,HTML,CSS,Javascript,MySQL,Jquery

Tuesday, 13 August 2019

PHP Insert Data into MySQLi Database

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

CREATE TABLE `person` ( `id` int(11) NOT NULL, `firstname` varchar(100) NOT NULL, `lastname` varchar(100) NOT NULL, `email` varchar(100) NOT NULL, `age` varchar(50) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `person` ADD PRIMARY KEY (`id`); ALTER TABLE `person` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

PHP insert data into mysql database
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.

html form to 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.
<html> <head> <title>Insert Data into Database Using PHP</title> <link href="../tutorials/style/bulma-0.7.4/css/bulma.css" rel="stylesheet" type="text/css"></link> <link href="../tutorials/style/bulma-0.7.4/css/bulma.min.css" rel="stylesheet" type="text/css"></link> </head> <body> <section class="hero is-fullheight"> <div class="container has-text-centered"> <div class="column is-8 is-offset-2"> <form action="insert.php" method="post"> <div class="box"> <h1 class="title"> Insert Data into Database Using PHP</h1> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> <label class="label">First Name</label> <input class="input is-info" name="txtfname" type="text" value="" /> </div> </div> <div class="field"> <div class="control"> <label class="label">Last Name</label> <input class="input is-info" name="txtlname" type="text" value="" /> </div> </div> </div> </div> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> <label class="label">Email Address</label> <input class="input is-info" name="txtemail" type="text" value="" /> </div> </div> <div class="field"> <div class="control"> <label class="label">Age</label> <input class="input is-info" name="txtage" type="text" value="" /> </div> </div> </div> </div> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> </div> </div> <div class="field"> <div class="control"> <input class="button is-info is-large is-fullwidth" name="btninsert" type="submit" value="Submit" /> </div> </div> <div class="field"> <div class="control"> </div> </div> </div> </div> </div> </form> </div> </div> </section> </body> </html>

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.
if(isset($_POST['btninsert'])) { $conn=mysqli_connect("localhost","root","","test"); if(mysqli_connect_error()) { die("Failed to connect with Database"); } $fname=mysqli_real_escape_string($conn,$_POST['txtfname']); $lname=mysqli_real_escape_string($conn,$_POST['txtlname']); $email=mysqli_real_escape_string($conn,$_POST['txtemail']); $age=mysqli_real_escape_string($conn,$_POST['txtage']); $sql="INSERT INTO `person`( `firstname`, `lastname`, `email`, `age`) VALUES ('$fname','$lname','$email','$age')"; $query=mysqli_query($conn,$sql); if($query) { echo "Data Saved Successfully"; } else { echo "Failed to Save data"; } }
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