PostgreSQL PHP: Insert Data Into Tables
Summary: in this tutorial, you will learn how to use PHP PDO API to insert data into a PostgreSQL database table.
Steps for inserting data into a PostgreSQL table using PDO
To insert data into a database table, you use the following steps:
- First, connect to the PostgreSQL database server by creating a new instance of the PDO class.
- Next, construct an INSERT statement. If you want to pass parameters to the
INSERT
statement, you use the named placeholders such as:param
- Then, prepare the
INSERT
statement by calling theprepare()
method of the PDO object. Theprepare()
method returns aPDOStatement
object. - After that, pass the values to the statement by calling the
bindValue()
method of thePDOStatement
object. - Finally, call the
execute()
method of the PDOStatement object to execute theINSERT
statement.
We will use the stocks
table that we created in the previous tutorial for demonstration purposes.
Let’s create a new class named PostgreSQLPHPInsert
in the app
folder and the index.php
file in the project folder.
Inserting a single row into a table example
The following insertStock()
method inserts a new row into the stocks
table.
First, construct an INSERT
statement that uses two named placed holders: :symbol
and :company
for binding values later.
Next, prepare the insert statement for execution by calling the prepare()
method of the PDO object.
Then, passing the values to the statement by calling the bindValue()
method.
After that, execute the INSERT
statement by calling the execute()
method.
Finally, get the ID of the last inserted row by calling the lastInsertId()
method of the PDO object
the PDO_PGSQL
extension requires us to specify the name of the sequence object as the parameter, we passed the stocks_id_seq
string to the function to get the generated ID.
Insert multiple rows into a table example
The following insertStockList()
method inserts multiple rows into the stocks
table.
The method accepts an array of stocks and calls the execute()
method multiple times to insert multiple rows into the stocks
table. It returns a list of inserted IDs.
Place the following code in the index.php file to test the insertStock()
and insertStockList()
methods.
Launch the index.php in the web browser, we got the following output:
In this tutorial, you have learned how to insert a single row or multiple rows into a table in the PostgreSQL database using PHP PDO.