PHP MYSQL : Insert Records

The INSERT INTO statement is used to insert new records to your database table.

Syntax

Three possible ways on how to use INSERT INTO statement

First way on how to insert records into your mysql table
doesn't specify the column only the data will be inserted.
INSERT INTO table_name VALUES (value1, value2)

Second way on how to insert records into your mysql table
INSERT INTO table_name (column1, column2) VALUES (value1, value2)

Third way on how to insert records into your mysql table
INSERT INTO table_name
SET
column1 = value1,
column2 = value2




Example code on how to Insert records using PHP in MYSQL database server.

<?php

// variables
$user = "localhost";
$username = "root";
$password = "password";

// connect to database
$db = new mysqli($user,$username,$password);
$db->select_db('your_databse');
        if (mysqli_connect_errno()) {
                die( "Connection failed..." );
        }

// php mysql insert records
$insert = "INSERT INTO `tbl_users`
                         (`id`,`username`,`password`,`firstname`,`lastname`)
               VALUES
                         ('1','myusername','newpassword','newname','mylastname')";
if (mysqli_query($db, $insert) === TRUE) {
          $success = true;
}  


?>

0 comments: