我无法使用 Postgre 和 PHP 插入数据

I cannot insert a data with Postgre and PHP

我正在使用 PHP 和 Postgre 开发软件。所以,我的插入代码有问题:

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;
    }
}

错误信息:

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

SQL 查询中的字符串值需要用撇号或引号引起来。将代码的第二行修改为:

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

编辑:当您在查询中使用字符串值并且未将其括在引号 ("test") 或撇号 ('test') 中时,PostgreSQL 将其作为列的名称,因此出现错误消息。