PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL PHP / PostgreSQL PHP: Updating Data In a Table

PostgreSQL PHP: Updating Data In a Table

Summary: in this tutorial, you will learn how to update data in a PostgreSQL database table using PHP PDO.

Steps for updating data in a database table from a PHP application

To update data in a table, you use these steps:

  1. Connect to the PostgreSQL database server by creating an instance of the PDO class.
  2. Call the prepare() method of the PDO object to prepare the UPDATE statement for execution. The prepare() method returns a PDOStatement object.
  3. Pass the values to the UPDATE statement by calling the bindValue() method of the PDOStatement object.
  4. Execute the UPDATE statement by calling the execute() method of the PDOStatement object.
  5. Get the number of rows updated using the rowCount() method of the PDOStatement object.

Updating data example

We will use the stocks table that we created in the creating table tutorial for the demonstration.

The updateStock() method of the PostgreSQLPHPUpdate class updates the data in the stocks table based on a specified id.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
 
namespace PostgreSQLTutorial;
 
/**
* PostgreSQL PHP Update Demo
*/
class PostgreSQLPHPUpdate {
 
    /**
     * PDO object
     * @var \PDO
     */
    private $pdo;
 
    /**
     * Initialize the object with a specified PDO object
     * @param \PDO $pdo
     */
    public function __construct($pdo) {
        $this->pdo = $pdo;
    }
 
    /**
     * Update stock based on the specified id
     * @param int $id
     * @param string $symbol
     * @param string $company
     * @return int
     */
    public function updateStock($id, $symbol, $company) {
 
        // sql statement to update a row in the stock table
        $sql = 'UPDATE stocks '
                . 'SET company = :company, '
                . 'symbol = :symbol '
                . 'WHERE id = :id';
 
        $stmt = $this->pdo->prepare($sql);
 
        // bind values to the statement
        $stmt->bindValue(':symbol', $symbol);
        $stmt->bindValue(':company', $company);
        $stmt->bindValue(':id', $id);
        // update data in the database
        $stmt->execute();
 
        // return the number of row affected
        return $stmt->rowCount();
    }
}

We use the PostgreSQLPHPUpdate class in the index.php file as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
 
require 'vendor/autoload.php';
 
use PostgreSQLTutorial\Connection as Connection;
use PostgreSQLTutorial\PostgreSQLPHPUpdate as PostgreSQLPHPUpdate;
 
try {
    // connect to the PostgreSQL database
    $pdo = Connection::get()->connect();
 
    //
    $updateDemo = new PostgreSQLPHPUpdate($pdo);
 
    // insert a stock into the stocks table
    $affectedRows = $updateDemo->updateStock(2, 'GOOGL', 'Alphabet Inc.');
 
    echo 'Number of row affected ' . $affectedRows;
} catch (\PDOException $e) {
    echo $e->getMessage();
}

In the index.php script, we connected to the PostgreSQL database and call the updateStock method of the PostgreSQLPHPUpdate class to update company name of the stock id 2 from Google Inc. to Alphabet Inc.

PostgreSQL PHP Update

Before running the script, we query data from the stocks table to see its current data.

1
2
3
4
SELECT
    id, symbol, company
FROM
    stocks;

1
2
3
4
5
6
7
id | symbol |        company
----+--------+-----------------------
  1 | MSFT   | Microsoft Corporation
  2 | GOOG   | Google Inc.
  3 | YHOO   | Yahoo! Inc.
  4 | FB     | Facebook, Inc.
(4 rows)

Launch the index.php file in a web browser, we get the following output.

1
Number of row affected 1

Let’s check the stocks table again.

1
2
3
4
5
6
7
id | symbol |        company
----+--------+-----------------------
  1 | MSFT   | Microsoft Corporation
  2 | GOOGL  | Alphabet Inc.
  3 | YHOO   | Yahoo! Inc.
  4 | FB     | Facebook, Inc.
(4 rows)

The company name of stock with id 2 has been updated to the new one.

In this tutorial, we have shown you how to update data in a PostgreSQL table using prepared statement in PHP PDO.

Previous Tutorial: PostgreSQL PHP: Insert Data Into Tables
Next Tutorial: PostgreSQL PHP: Querying Data

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL PHP

  • Connect to PostgreSQL Database Using PDO
  • Create New Tables in PHP
  • Insert Data Into Tables in PHP
  • Update Data In a Table using PDD
  • Handle Transaction in PHP
  • Query Data From PostgresQL using PDO
  • Call PostgreSQL Stored Procedures in PHP
  • Manage with BLOB in PHP
  • Delete Data From a Table in PHP

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.