marcelps
marcelps

Reputation: 319

I cannot insert a data with Postgres and PHP

I'm developing a software with PHP and Postgres. 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);
    
    if($result){
        return true;
    }else{
        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

Upvotes: 0

Views: 513

Answers (1)

Jirka Hrazdil
Jirka Hrazdil

Reputation: 4021

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.

Upvotes: 3

Related Questions