Insert Data MySQL Database Using PHP PDO Method

In this tutorial how to insert data in a MySQL table using PDO Method in PHP

MySQL INSERT Query using PDO Method

We will discuss PHP PDO Method. Example is given below.

This new PDO() function is establish database connection. Next set POD error mode. $e variable show database connecting error. INSERT INTO sql query insert data Mysql database table. $pdo->exec($sql) function executed INSERT INTO sql query. Then close connection.

Database Structure example is given below

database table structure
database table structure

PDO Handle Errors

The three type error modes manage the error mode attribute in your newly created database connection.

  • PDO::ERRMODE_SILENT
  • PDO::ERRMODE_WARNING
  • PDO::ERRMODE_EXCEPTION

Example is given below

Three methods are writing in the INSERT INTO statement. Procedural Method and Object Oriented Method as we discussed earlier.