gpt4 book ai didi

php - 如何从 PHP 向 Postgres 插入空值

转载 作者:行者123 更新时间:2023-11-29 12:12:39 25 4
gpt4 key购买 nike

我一直在开发一个项目管理系统,我正在使用 PHP 和 PostgreSQL 8.4 以及模型- View - Controller 对其进行编码。

为了让你们了解上下文,我将从根本上解释(尽可能简短)。

我有一个名为 Actividad 的类,它扩展了一个数据库抽象类来执行查询。

    class Actividad extends AbstraccionBD
{

/**
* Constructor de la clase
*
* @param String $nom Nombre
* @param String $des Descripción
* @param String $fi Fecha de inicio
* @param String $fc Fecha de culminación
* @param Float $pon Ponderación
* @param Boolean $crit Crítica
* @param String $le Lugar Ejeucución
* @param String $rec Recursos
* @param Integer $pe Presupuesto estimado
* @param String $est Estado
* @param Integer $cal Calificación
* @param Integer $peje Presupuesto ejecutado
* @param Integer $asis Asistencia
*/
public function __construct($nom, $des, $fi, $fc, $pon, $crit, $le, $rec,
$pe, $est, $cal, $peje, $asis)
{

//Asignamos todos los valores especificados al
//objeto
$this->nombre = $nom;
$this->descripcion = $des;
$this->fecha_inicio = $fi;
$this->fecha_culminacion = $fc;
$this->ponderación = $pon;
$this->es_critica = $crit;
$this->lugar_ejecucion = $le;
$this->recursos = $rec;
$this->presupuesto_est = $pe;
$this->estado = $est;
$this->calificacion = $cal;
$this->presupuesto_ejec = $peje;
$this->asistencia = $asis;

}

public function insertarObjeto()
{
//Construimos el query de inserción de datos en la tabla
//actividad; nótese que se llama al procedimiento almacenado
//sga.max_num_actividad(int,int) para determinar el número
//de la actividad que se esta insertando.
$this->_query =
"INSERT INTO sga.actividades " .
"(proyecto, ejec_proyecto, num_actividad, nombre, " .
"descripcion, fecha_inicio, fecha_culminacion, " .
"ponderacion, critica, lugar_ejecucion, recursos, " .
"prepuesto_estimado, estado, calificacion, " .
"presupuesto_ejecutado, asistencia) " .
"VALUES " .
"($this->proyecto, $this->ejec_proyecto, " .
"sga.max_num_act($this->proyecto, $this->ejec_proyecto) + 1, " .
"'$this->nombre', " .
"'$this->descripcion', '$this->fecha_inicio', " .
"'$this->fecha_culminacion', $this->ponderación, " .
"$this->es_critica, '$this->lugar_ejecucion', " .
"'$this->recursos', $this->presupuesto_est, " .
"'$this->estado', $this->calificacion, " .
"$this->presupuesto_ejec, $this->asistencia);";

//Ejecutamos el query de inserción
$this->ejecutarQuery();
}

(我被要求为项目每行保留 80 个字符。)

表定义(不考虑FK):

CREATE TABLE sga.actividades
(
proyecto integer NOT NULL,
ejec_proyecto integer NOT NULL,
num_actividad smallint NOT NULL,
nombre character varying(150),
descripcion character varying(500),
fecha_inicio date NOT NULL,
fecha_culminacion date NOT NULL,
ponderacion numeric (3,2) NOT NULL DEFAULT 0.00,
critica boolean NOT NULL DEFAULT FALSE,
lugar_ejecucion character varying(100),
recursos character varying(250),
prepuesto_estimado integer,
estado sga.estados_actividad NOT NULL, -- Dominio estados_actividad
calificacion integer,
presupuesto_ejecutado integer,
asistencia smallint,
CONSTRAINT actividad_pkey
PRIMARY KEY(proyecto, ejec_proyecto, num_actividad)
);

现在,我要做的是传递模型中的值,像这样的“东西”:

    $a = new Actividad('Actividad1','DescA1', '14-07-14','14-07-14',0.00, 
'false',null,null,0,'ACT',null, null, null);

//Dont worry im using __set method
$act->proyecto = 1;
$act->ejec_proyecto = 1;

$a->insertarObjeto();

如您所见,我在构造函数中传递了一些 NULL 值,因为在数据库中这些值可以为 null,到目前为止一切都很酷。

当我尝试运行它时,我得到了这个查询:

INSERT INTO sga.actividades 
(proyecto, ejec_proyecto,
num_actividad, nombre,
descripcion, fecha_inicio,
fecha_culminacion,
ponderacion, critica,
lugar_ejecucion, recursos,
prepuesto_estimado, estado,
calificacion, presupuesto_ejecutado,
asistencia)
VALUES
(1, 1, sga.max_num_act(1, 1) + 1,
'Actividad1', 'DescA1', '14-07-14',
'14-07-14', 0, false, '', '', 0, 'ACT', , , );

这是我的问题:这个查询永远不会运行,因为我正在使用 PHP 中的 NULL 关键字,当它在“疯狂连接”中转换为字符串时,它保持为空(字符串中没有任何内容) ),因此 Postgres(如预期的那样)在 (, , ,) 部分发送语法错误。

我需要用 NULL 关键字替换那些空字符串 (''),以便 Postgres 正确识别和插入它。

此外,构造函数中传递的一些值在 insertarObjeto() 函数中用单引号括起来(它们在数据库中是字符变化)。

并且在 Postgres 中 ''NULL 不同。

我试图解决这个问题

一种方法是放置 (N) 个 if-else 语句并在每个情况下连接正确的(这对于将来维护代码和系统扩展来说有点丑陋),像这样:

if(is_null($this->asistencia))
{
$this->_query .= "null, ";
}
else
{
$this->_query .= "$this->asistencia, ";
}

这种方法的问题是属性太多(我的类比这个多得多)。

我看到的另一种方法是在构造函数中用单引号将 null 括起来。这适用于整数(我知道这是一个糟糕的解决方案),但是函数中已经用单引号包裹的那些将在查询中抛出“null”,这也是错误的,即:

INSERT INTO sga.actividades 
(proyecto, ejec_proyecto,
num_actividad, nombre,
descripcion, fecha_inicio,
fecha_culminacion,
ponderacion, critica,
lugar_ejecucion, recursos,
prepuesto_estimado, estado,
calificacion, presupuesto_ejecutado,
asistencia)
VALUES
(1, 1, sga.max_num_act(1, 1) + 1,
'Actividad1', 'DescA1', '14-07-14',
'14-07-14', 0, false, 'null', 'null', 0, 'ACT', null ,null ,null );

有更好的解决方案吗?除了 (n) 个 if-else 语句之外?

最佳答案

两种可能的解决方案:

1。省略列

如果您没有定义列默认值,则默认默认值(原文如此!)为 NULL。您可以只省略应为 NULL 的列:

INSERT INTO sga.actividades 
(proyecto, ejec_proyecto,
num_actividad, nombre,
descripcion, fecha_inicio,
fecha_culminacion,
ponderacion, critica,
prepuesto_estimado, estado)
VALUES
(1, 1, sga.max_num_act(1, 1) + 1,
'Actividad1', 'DescA1', '14-07-14',
'14-07-14', 0, false, 0, 'ACT');

当然,如果您更改此处未提及的列的列默认值,您也会更改此 INSERT 的结果 - 这可能是合意的,也可能不是合意的。

2。使用准备好的语句

然后您可以使用 PHP NULL 值。无论如何,清理输入并防止可能的 SQL 注入(inject)是一个好主意。

pg_prepare($pg_conn, 'insert1', "INSERT INTO sga.actividades (proyecto, ejec_proyecto, ..., lugar_ejecucion, ...) VALUES ($1, $2, ...)");

pg_exec($pg_conn, 'insert1', array(1, 1, ..., NULL, ...));

手册中有更多信息 pg_preparepg_execute .

关于php - 如何从 PHP 向 Postgres 插入空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com