PHP

Updating Data in PostgreSQL

pg_insert()

Another way to insert or update data in PostgreSQL comes in the form of the functions pg_insert() and pg_update().

The first parameter must be the database handle, the second parameter is the table to be inserted into/updated, and the third parameter contains some data in the form of an associative array (column names are the keys). In the event of an UPDATE SQL statement, the update condition must also be submitted as an array in the fourth parameter of the function. The preceding code shows how to insert data.

Sending SQL to PostgreSQL
<?php
  if ($db = @pg_connect('host=localhost port=5432
    dbname=hoshmand user=postgres
    password=abc123')) {
    require_once 'stripFormSlashes.inc.php';
    $data = array(
      'quote' => pg_escape_string($_POST['quote']),
      'author' => pg_escape_string($_POST
         ['author']),
      'year' => intval($_POST['year'])
    );
    pg_insert($db, 'quotes', $data);
    echo 'Quote saved.';
    pg_close($db);
  } else {
    echo 'Connection failed.';
  }
?>

by BrainBellupdated
Advertisement: