How To Use SQL INSERT INTO Statement In Query Using PHP

admin_img Posted By Bajarangi soft , Posted On 18-09-2020

INSERT INTO Statement will add new values to table

insert_into

SQL INSERT INTO Statement

  • INSERT INTO statement is used to add new record to a table of any database.
  • INSERT INTO adds a new record to a table.
  • INSERT INTO can be combined with a SELECT to insert records.
 

INSERT INTO statement Syntax  and Usage.

There are two basic way syntaxes of the INSERT INTO statement.

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);

1. In this above insert statement by using table columns we can add new values to table .
 
INSERT INTO table_name
VALUES (value1, value2, value3, ...);

2.In this above insert statement by using table name we can add new values to table .

Lets learn

1.Create database and table.
student table
StudentID StudentName ParentsContact Address Country DOB Section

Branch


  1
    Liam 10101010 23 MG road
Banglore  
INDIA 12/06 A CSE 
  2    Andrew 20202020 2415 Anthony  market str Germany 01/02 A ISE 
  3    Joshua 30303030 26547 David church road USA 18/04 B CL 
 4     Ryan 40404040 18 Krishna sq.
Banglore
INDIA 26/12 B EE
 5    Charles 50505050 878 Eli Berguvsvägen 8 INDIA 23/11 C ECE

Now for this student table we are going to add new  data using INSERT INTO  statement.
 

INSERT INTO Example(one way).
SQL statement inserts a new record in the "student" table using column names .

INSERT INTO Student (StudentName, ParentsContact,Address, Country, DOB,Section,Branch)
VALUES ('leo', '12345678', 'Skagen 21',  'USA','15/12','C','ME');

INSERT INTO Example(another way).
SQL statement inserts a new record in the "student" table using wihtout using  column  names .
INSERT INTO Student VALUES ('Jack', '12345688', 'washington 21',  'USA','12/02','D','ME' );

2.Complete code of SQL INSERT INTO  Statement in PHP script.
<!DOCTYPE html>
<html lang="en">
<head>
    <title>How to use SQL INSERT INTO Statement</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>How to use SQL INSERT INTO Statement</h1>
    </div>
    <div class="well">
        <?php
        $connect = mysqli_connect("localhost", "root", "", "school_management");
        if($connect === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        }

        $sql = "INSERT INTO Student (StudentName, ParentsContact,Address, Country, DOB,Section,Branch)
VALUES ('leo', '12345678', 'Skagen 21',  'USA','15/12','C','ME')";
        if(mysqli_query($connect, $sql)){
            echo "New Records added successfully.";
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($connect);
        }
        mysqli_close($connect);
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post