0

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.