I'm trying to write an "INSERT" into a webpage using phpfor a postgres database. This is to input some data from a registration form written in PHP. Currently i have this code in PHP:
function insert($table, $data)
{
global $db;
if ($table and is_array($data) and !empty($data))
{
$columns = implode(',',array_keys($data));
$values = implode(',', escape($data));
$sql = "INSERT INTO {$table} ($columns) VALUES ($values)";
$q = pg_query($db, $sql) or db_error_out();
$result = pg_query($db, $sql) or db_error_out();
$insert_row = pg_fetch_row($result);
$id = $insert_row[0];
return ($id > 0) ? $id : FALSE;
} else {
return FALSE;
}
}
The code works and is inserting the row into postgres DB but i also get this error:
Warning: pg_query(): Query failed: ERROR: duplicate key value violates unique constraint "index_clients_on_first_name_and_last_name_and_phone_and_email"
DETAIL: Key (first_name, last_name, phone, email)=(test, test, 5461230, test@mail.com) already exists. in C:\xampp\htdocs\webpage_dev\system\database.php on line 165
Line 165 is the $result, where i know that $db works correctly so i assume that $sql is faulty.
My question is how to get rid of this error and help with the INSERT.