0% found this document useful (0 votes)
13 views

PHP MySQL Insert Data

The document discusses inserting data into a MySQL database using PHP. It provides the SQL syntax for INSERT statements, examples of inserting a record into a table using MySQLi and PDO, and notes that auto-increment and timestamp columns do not need to be specified in the query.

Uploaded by

Saripilli Vasu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

PHP MySQL Insert Data

The document discusses inserting data into a MySQL database using PHP. It provides the SQL syntax for INSERT statements, examples of inserting a record into a table using MySQLi and PDO, and notes that auto-increment and timestamp columns do not need to be specified in the query.

Uploaded by

Saripilli Vasu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PHP 

MySQL Insert Data


❮ PreviousNext ❯

Insert Data Into MySQL Using MySQLi and


PDO
After a database and a table have been created, we can start adding data in
them.

Here are some syntax rules to follow:

 The SQL query must be quoted in PHP


 String values inside the SQL query must be quoted
 Numeric values must not be quoted
 The word NULL must 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 visit 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
us fill the table with data.

Note: If a column is AUTO_INCREMENT (like the "id" column) or TIMESTAMP


with default update of current_timesamp (like the "reg_date" column), it is
no need to be specified in the SQL query; MySQL will automatically add the
value.

The following examples add a new record to 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();
?>

You might also like