ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

09-MySQL Insert Query

To insert record in MySQL database the statement used is INSERT INTO command.

Syntax:

INSERT INTO name_of_table (column1, column2, column3,…)
VALUES (value1, value2, value3,…)

 

Rules in performing the INSERT INTO Command:

  • A Numeric Values and NULL word should not be quoted
  • String Values inside SQL query should be quoted
  • Using PHP all SQL query should be quoted
  • No Need to specify a for a column that is SET to AUTO_INCREMENT and TIMESTAMP

 

In last tutorial called 08-MySQL Create Table, we created a blank table named ‘tblpeople’ . This time, we’re going to populate this table.

 

Here are the different examples on how to populate the ‘tblpeople’:

 

Example using MySQLi (Object-Orient):

 

  1. <?php
  2. $server = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $database = "mysqltutorial";
  6.  
  7. // Create connection
  8. $conn = new mysqli($server, $username, $password, $database);
  9. // Check connection
  10. if ($conn->connect_error) {
  11. die("Connection failed: " . $conn->connect_error);
  12. }
  13.  
  14. // sql to Insert record table
  15. $sql = "INSERT INTO `tblpeople` (`LNAME`, `FNAME`, `ADDRESS`)
  16. VALUES ('Villanueva', 'Joken', 'Kabankalan City');";
  17.  
  18. if ($conn->query($sql) === TRUE) {
  19.  echo "New record created successfully";
  20. } else {
  21.  echo "Error: " . $sql . "<br>" . $conn->error;
  22. }
  23.  
  24. $conn->close();
  25. ?>

 

Example using MySQLi (Procedural):

 

  1. <?php
  2. $server = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $database = "mysqltutorial";
  6.  
  7. // Create connection
  8. $conn = mysqli_connect($server, $username, $password, $database);
  9. // Check connection
  10. if (!$conn) {
  11.     die("Connection failed: " . mysqli_connect_error());
  12. }
  13.  
  14. // sql to Insert record table
  15. $sql = "INSERT INTO `tblpeople` (`LNAME`, `FNAME`, `ADDRESS`)
  16. VALUES ('Villanueva', 'Joken', 'Kabankalan City');";
  17.  
  18. if (mysqli_query($conn, $sql)) {
  19.     echo "New record created successfully";
  20. } else {
  21.     echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  22. }
  23.  
  24. mysqli_close($conn);
  25. ?>

 

Example using PDO:

 

  1. <?php
  2. $server = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $database = "mysqltutorial";
  6.  
  7. try {
  8.     $conn = new PDO("mysql:host=$server;dbname=$database", $username, $password);
  9.     // set the PDO error mode to exception
  10.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11.  
  12.     // sql to Insert record table
  13.     $sql = "INSERT INTO `tblpeople` (`LNAME`, `FNAME`, `ADDRESS`)
  14.     VALUES ('Villanueva', 'Joken', 'Kabankalan City');";
  15.  
  16.     // use exec() because no results are returned
  17.     $conn->exec($sql);
  18.     echo "New record created successfully";
  19.     }
  20. catch(PDOException $e)
  21.     {
  22.     echo $sql . "<br>" . $e->getMessage();
  23.     }
  24.  
  25. $conn = null;
  26. ?>
Facebook Comments
  • books
    books
  • mysql-php
    mysql-php

Check Your Domain Ranking

Leave a Reply

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

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!