How to insert data in MYSQL using PHP

How-to-insert-data-in-MYSQL-using-PHP

How to insert data in MYSQL using PHP

In this tutorial, you learn how to create database and tables in MySQL and how to execute SQL query to insert records into a table of Database.

There are few steps for inserting a record in database :-

  • Create a Table in MYSQL .
  • Create a Connection file ,  which is used for connecting the PHP to phpMyAdmin.
  • Create a FORM in HTML and give Style Using CSS .
  • In the Form - create Input fields and submit button .
  • Create a 'insert' query to insert the record in the database.
  • Now check the Database in phpMyAdmin Panel and see the record is inserted in the database correctly .

1. So, To start first open an editor, Create a TABLE Using SQL  and named it as `table` then create a Auto Increment `id` , username , email , data  or  Paste the following code and save it as .sql or the extension you want to save.(Database making)

CREATE TABLE `table` (
  `id` int(6) UNSIGNED NOT NULL,
  `username` varchar(30) NOT NULL,
  `email` varchar(50) DEFAULT NULL,
  `data` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `table`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `table`
MODIFY `id` int(6) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;

2.  The Second Step is to make a Database Connection File in PHP which is used to connecting the PHP to MYSQL  .  Create a File index.php or the extension you want to save .

<?php

$conn=mysqli_connect("localhost","root","","mydatabase");

if(!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
?>

 

3. The third Step is to make a FORM in HTML by using Form Tag . Paste the following code in the same file which is index.php or the extension you want to save.

<!Doctype html>
<html>
<head>
</head>
<body>
<form action="" method="post"> 
<label id="first"> First name:</label><br/>
<input type="text" name="username"><br/>
<label id="first">Email</label><br/>
<input type="text" name="email"><br/>
<label id="first">Data</label><br/>
<input type="text" name="data"><br/>
<button type="submit" name="save">save</button>
</form>
</body>
</html>

 

4. The Forth Step is to make a Php code which is used to inserting a record in Database by `INSERT` Command in SQL . Paste the following code in the same file which is index.php or the extension you want to save.

<?php

$conn=mysqli_connect("localhost","root","","mydatabase");

if(!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST['save']))
{
    $sql = "INSERT INTO table (username,email,data)
    VALUES ('".$_POST["username"]."','".$_POST["email"]. "','".$_POST["data"]."')";

    $result = mysqli_query($conn,$sql);
}

?>


5. The Fifth Step is to check the record that is inserted in the database.

  • Go to Url and type `localhost/phpmyadmin
  • Select the Database the you had already Created
  • Click on table name as `table`
  • Check the Entries
  • Enjoy


STAY CONNECTED FOR MORE :)

ADD COMMENT

Your email address will not be published. Required fields are marked *







John
13/01/2018

Good Article

Briz ti
14/01/2018

Sir how to create database ?

Gurdeep Singh
15/01/2018

Very good article and nice post regarding PHP and Mysql

The primary motive of our company is to provide beyond satisfactory results .

Request a free consultation quote

WWW.BHAGAUTI.COM © COPYRIGHT 2018 | TERMS AND CONDITIONS | Privacy & Policy