How to insert data into database table in php language?

Inserting Data into a MySQL Database Table

Now that you’ve understood how to create database and tables in MySQL. In this tutorial you will learn how to execute SQL query to insert records into a table.
The INSERT INTO statement is used to insert new rows in a database table.
Let’s make a SQL query using the INSERT INTO statement with appropriate values, after that we will execute this insert query through passing it to the PHP mysqli_query() function to insert data in table. Here’s an example, which insert a new row to the data table by specifying values for the title and content fields.

 

Step 1 :- First create database table

CREATE TABLE `data` (
 `id` int(100) NOT NULL,
 `title` varchar(100) NOT NULL,
 `content` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step 2:-Then Create db connection in php and mysqli with insert query in index.php file

Index.php

<html>
<title>Registration</title>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<form method="post" action="">
 <div class="container">
 <h1 class="well">insert data</h1>
 <div class="col-lg-12 well">
 <div class="row">
 <div class="col-sm-12">
 <div class="row">
 <div class="col-sm-6 form-group">
 <label>Title</label>
 <input type="text" name="title" id="name">
 </div>
 <div class="col-sm-6 form-group">
 <label>Content</label>
 <textarea name="content" class="form-control"></textarea>
 </div>
 </div> 
 <button type="submit" name="Register" value="Register" class="btn btn-primary btn-block">Register</button> 
 </div>
 </div>
 </div>
 </div>
</form> 
</body>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "register";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
 die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST['Register']))
{
 $title=$_POST['title'];
 $content=$_POST['content'];
 $sql1 = "INSERT INTO data(title, content) VALUES ('$title', '$content')";
 if(mysqli_query($conn, $sql1)){ 
 echo "New record created successfully";
 } 
 }
mysqli_close($conn);
?>