2017-03-31 44 views
0

我呼籲art_movimientosPostgreSQL的文本字段獲得'時,他們是空

CREATE TABLE public.art_movimientos 
(
    cmovimiento bigint NOT NULL DEFAULT nextval('art_movimientos_cmovimiento_seq'::regclass), 
    tipo character varying(3) NOT NULL, -- Tipos de Valores:... 
    fecha_mov timestamp without time zone NOT NULL, 
    documento integer NOT NULL, 
    control integer, 
    fecha_doc timestamp without time zone NOT NULL, 
    corden integer NOT NULL DEFAULT 0, 
    calmacen integer NOT NULL, 
    calmacen2 integer, 
    status character varying(13) NOT NULL DEFAULT 'PENDIENTE'::bpchar, -- PENDIENTE... 
    donado integer NOT NULL DEFAULT 0, 
    monto_mov numeric(11,2) NOT NULL DEFAULT 0.00, 
    monto_desc numeric(11,2) NOT NULL DEFAULT 0.00, 
    monto_total numeric(11,2) NOT NULL DEFAULT 0.00, 
    observacion text, 
    casiento integer, 
    crea_user character varying(25), 
    crea_date timestamp without time zone, 
    mod_user character varying(25), 
    mod_date timestamp without time zone, 
    cproveedor integer NOT NULL DEFAULT 0, 
    CONSTRAINT pk_cmovimiento_art_movimientos PRIMARY KEY (cmovimiento) 
    USING INDEX TABLESPACE sistema_index, 
    CONSTRAINT fk_calmacen_art_movimientos FOREIGN KEY (calmacen) 
     REFERENCES public.almacen (calmacen) MATCH SIMPLE 
     ON UPDATE RESTRICT ON DELETE RESTRICT, 
    CONSTRAINT ck_donado_art_movimientos CHECK (donado = ANY (ARRAY[0, 1])), 
    CONSTRAINT ck_monto_total_art_movimientos CHECK (monto_total > 0::numeric AND monto_total >= monto_mov), 
    CONSTRAINT ck_status_art_movimientos CHECK (status::text = ANY (ARRAY['PENDIENTE'::character varying, 'PROCESADO'::character varying, 'APROBADO'::character varying, 'CONTABILIZADO'::character varying]::text[])), 
    CONSTRAINT ck_tipo_art_movimientos CHECK (tipo::text = ANY (ARRAY['AJE'::character varying, 'AJS'::character varying, 'SI'::character varying, 'CSM'::character varying, 'COI'::character varying, 'COM'::character varying, 'DEV'::character varying, 'TRF'::character varying]::text[])) 
) 
WITH (
    OIDS=FALSE 
); 

控制器中的表中獲取數據,並在數組轉換爲傳遞給類:

...  
$_obs =$_POST['observacion']<>'' ? strtoupper($_POST['observacion']) : $_POST['observacion']; 
.... 
array_push($values, $_obs); 
... 
$mov_dat = $mov->new_mov($values); 

的DB類具有該功能,它調用另一個函數並返回結果:

public function new_mov($array){ 
      return $mov = $this->db->procedure("insert_art_mov",$array); 
     } 
... 
public function procedure($procedure=false,$rows=false){ 
     $response=array(); 
     if($procedure==false){ 
      $response="Query is Empty!"; 
     }else{ 
      $sql = "SELECT ".$procedure."("; 
      for ($i=1; $i<=count($rows); $i++){ 
       $sql .= "$".$i.","; 
      } 
      $sql = substr($sql,0,-1); 
      $sql .= ")"; 
      //echo $sql; 
      $res = pg_prepare($this->linkid, "my_query", $sql); 
      $res = @pg_execute($this->linkid, "my_query", $rows); 
      if($res){ 
       while($consF=pg_fetch_assoc($res)) 
        array_push($response,$consF[$procedure]); 
      }else{ 
       array_push($response,"ERROR"); 
       array_push($response,$this->validateOperation()); 
      } 
      $sql = sprintf('DEALLOCATE "%s"',pg_escape_string("my_query")); 
      if(!pg_query($sql)) { 
       die("Can't query '$sql': " . pg_last_error()); 
      } 
      return $response; 
     } 
    } 

然後我執行程序在數據庫中的dure:

CREATE OR REPLACE FUNCTION public.insert_art_mov(...IN _observacion text,..) 
INSERT INTO art_movimientos(tipo,fecha_mov,documento,control,fecha_doc,corden,cproveedor,calmacen,status,donado,monto_mov,monto_desc,monto_total,observacion,crea_user) 
      VALUES(_tipo,NOW(),_documento,_control,_fecha_doc,_corden,_cprov,_calmacen,'PENDIENTE',_donado,MONTO_MOV,_monto_desc,MONTO_TOT,_observacion,_crea_user) RETURNING cmovimiento INTO new_cmov; 

但在觀察表中我看到''(簡單cuotes),我想知道爲什麼?

回答

1

因爲您不檢查空字符串,您將得到這些單引號。我會推薦的,我也這樣做是爲每個字符串字段傳遞給函數返回數據或NULL。

DICLAIMER:一半的答案,但大多是指導:)

對於後端,你可以試試下面的功能時存在需要添加一個字符串:

function chech_string ($str) { 
    return (0 != strlen($str)) ? $str : NULL; 
} 

或者你創建SQL字符串作爲跟隨

$sql = "INSERT INTO table(col1, col2) VALUES ('; 

if (0 != strlen($str1)) 
    $sql .= "'$str1'"; 
else 
    $sql .= "NULL"; 

if (0 != strlen($str2)) 
    $sql .= "'$str2'"; 
else 
    $sql .= "NULL"; 

    $sql .= ')'; 

在結束你的字符串看起來像

$sql = "INSERT INTO table(col1, col2) VALUES ('STR1', 'STR2')"; 
OR 
$sql = "INSERT INTO table(col1, col2) VALUES ('STR1', NULL)"; 

所以,你可以使用這個mething以創建程序以及

對於前端:

<input type="text" value="<?php echo (0 != strlen($col_val)) ? $col_val : '' ?>"/> 

會給你你在前端需要的結果

+0

但我不想要NULLS或簡單的引號('')只是一個字段爲空 – JuJoGuAl

+1

您的數據庫是否返回NULLS或空字符串?因爲在postgresql中可以存儲爲emoty字符串,但是在任何一個cas中,您都可以在前端使用相同的邏輯,因此value ='<?php echo(0!= strlen($ col_val))? 'col_val:''?>' 這將給你你需要在前端的結果 – CAllen

+0

任何兩個,我的前端,顯示我什麼都沒有(空值),但如果有我需要做的查詢,爲選擇沒有數據的「觀察者」? SELECT * FROM art_movimientos WHERE observaciones =''? – JuJoGuAl

相關問題