Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

MySQL Insert Data

Insert Data Into MySQL Using MySQLi and PDO

Once a database and table are created, you can begin adding data to them.

Follow these syntax rules:

  • The SQL query must be enclosed in quotes in PHP.
  • String values within the SQL query must be enclosed in quotes.
  • Numeric values should not be quoted.
  • The word NULL should not be quoted.

The INSERT INTO statement is used to add new records to a MySQL table.

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

To learn more about SQL, please refer to our SQL tutorial.

In the previous chapter, we created an empty table named “MyGuests” with five columns: “id”, “firstname”, “lastname”, “email”, and “reg_date”. Now, let’s populate the table with data.

Note: If a column is set to AUTO_INCREMENT (such as the “id” column) or is a TIMESTAMP with a default update to CURRENT_TIMESTAMP (like the “reg_date” column), you do not need to specify it in the SQL query; MySQL will automatically provide the value.

The following examples insert a new record into the “MyGuests” table:

Example (MySQLi Object-oriented)

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDB”;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die(“Connection failed: “ . $conn->connect_error);
}

$sql = “INSERT INTO MyGuests (firstname, lastname, email)
VALUES (‘John’, ‘Doe’, ‘[email protected]’)”
;

if ($conn->query($sql) === TRUE) {
  echo “New record created successfully”;
else {
  echo “Error: “ . $sql . “<br>” . $conn->error;
}

$conn->close();
?>

Example (MySQLi Procedural)

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDB”;

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die(“Connection failed: “ . mysqli_connect_error());
}

$sql = “INSERT INTO MyGuests (firstname, lastname, email)
VALUES (‘John’, ‘Doe’, ‘[email protected]’)”
;

if (mysqli_query($conn, $sql)) {
  echo “New record created successfully”;
else {
  echo “Error: “ . $sql . “<br>” . mysqli_error($conn);
}

mysqli_close($conn);
?>

Example (PDO)

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDBPDO”;

try {
  $conn = new PDO(“mysql:host=$servername;dbname=$dbname”, $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $sql = “INSERT INTO MyGuests (firstname, lastname, email)
  VALUES (‘John’, ‘Doe’, ‘[email protected]’)”
;
  // use exec() because no results are returned
  $conn->exec($sql);
  echo “New record created successfully”;
catch(PDOException $e) {
  echo $sql . “<br>” . $e->getMessage();
}

$conn = null;
?>