Marcel Marcel -4 years ago 97
PHP Question

I cannot insert a data with Postgre and PHP

I'm developing a software with PHP and Postgre. So, I have a problem with insert code:

function create(){
$query = "INSERT INTO " . $this->table_name . " VALUES (" . $this->pess_nome . ")";

$result = pg_query($this->conn, $query);

return true;
return false;

The error message :

Warning: pg_query(): Query failed: ERROR: column "test" does not exist LINE 1: INSERT INTO pessoa VALUES (test) ^ in C:\xampp\htdocs\Projeto_GPass\objects\pessoa.php on line 26

Answer Source

String values in SQL queries need to be enclosed in apostrophes or quotes. Modify the second line of your code to read:

$query = "INSERT INTO " . $this->table_name . " VALUES ('" . $this->pess_nome . "')";

EDIT: When you use a string value in a query and do not enclose it in quotes ("test") or apostrophes ('test'), PostgreSQL takes it as a name of the column, hence the error message.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download