Ajax请求PHP保存方法

时间:2017-05-03 17:56:28

标签: php jquery ajax codeigniter

我使用 AJAX 发送了一系列值,我的框架字是 CODEIGNITER 3

这是方法

$.ajax({
          method: "POST",
          url: "<?php echo base_url()?>empleadoController/save_empleado",
          type:"POST",
          data: {
            "fecha_ingreso": fecha_ingreso,
            "direccion": direccion ,
            "nombrecompleto": nombrecompleto,
            "cedula": cedula ,
            "carnet":carnet ,
            "telefono": telefono ,
            "observaciones": observaciones ,
            "email": email,
            "dominioempleado": dominioempleado ,
            "usr_open": usr_open ,
            "usr_oda": usr_oda ,
            "usr_qflow": usr_qflow ,
            "usr_webclient": usr_webclient ,
            "usr_siv": usr_siv ,
            "tienda": tienda ,
            "empresa": empresa ,
            "funcion": funcion
           },

          success: function (data) {
            alert("Realizado");

          },
          error: function (data) {
           alert("Error");
          }

        });
      });

数据阵列成功获取信息,但在控制器中我一直收到此消息

 A Database Error Occurred

Error Number: 23502/7

ERROR: null value in column "idtienda" violates not-null constraint DETAIL: Failing row contains (716, null, null, null, SIN CEDULA, FIZUNCH, uploads/default-user.png, , , SIN TELEFONO, SIN DIRECCION, 01-01-1970, SIN OBSERVACIONES, t, SIN DOMINIO).

INSERT INTO "tbl_ctrl_empleados" ("idtienda", "idtipoempresa", "idfuncion", "cedula", "carnetempleado", "rutafoto", "nombrecompleto", "email", "telefono", "direccion", "fecha_ingreso", "observaciones", "estado", "dominioempleado") VALUES (NULL, NULL, NULL, 'SIN CEDULA', 'FIZUNCH', 'uploads/default-user.png', '', '', 'SIN TELEFONO', 'SIN DIRECCION', '01-01-1970', 'SIN OBSERVACIONES', TRUE, 'SIN DOMINIO')

Filename: C:/xampp/htdocs/MasterKey/system/database/DB_driver.php

Line Number: 691

但信息直接存在并保存

715;45;6;9;"0012208950011K";"010101";"uploads/default-user.png";"LUIS ALFONSO VELAZQUEZ FLOREZA";"LUID@HOTMAIL.COM";"22334411";"NO LO SE";"01-01-1970";"AQUI";TRUE;"AQUI"

请告诉我我做错了什么。

更新 我表中的架构是

CREATE TABLE key.tbl_ctrl_empleados
(
  idempleado serial NOT NULL,
  idtienda integer NOT NULL,
  idtipoempresa integer NOT NULL,
  idfuncion integer NOT NULL,
  cedula character varying(20) NOT NULL,
  carnetempleado character varying(100) NOT NULL,
  rutafoto character varying(300),
  nombrecompleto character varying(300) NOT NULL,
  email character varying(100) NOT NULL,
  telefono character varying(50) NOT NULL,
  direccion character varying(500) NOT NULL,
  fecha_ingreso character varying(50) NOT NULL,
  observaciones character varying(300) NOT NULL,
  estado boolean NOT NULL DEFAULT true,
  dominioempleado character varying(300) NOT NULL,
  CONSTRAINT tbl_ctrl_empleados_pkey PRIMARY KEY (idempleado),
  CONSTRAINT tbl_ctrl_empleados_idfuncion_fkey FOREIGN KEY (idfuncion)
      REFERENCES key.cat_ctrl_funcion (idfuncion) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT tbl_ctrl_empleados_idtienda_fkey FOREIGN KEY (idtienda)
      REFERENCES key.tbl_ctrl_tiendas (idtienda) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT tbl_ctrl_empleados_idtipoempresa_fkey FOREIGN KEY (idtipoempresa)
      REFERENCES key.ft_ctrl_rel_tipoempresa (idrelacion) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE key.tbl_ctrl_empleados
  OWNER TO postgres;

我的控制器中的方法:

function save_empleado() {
    $valoresReemplazoCedula = array(
    '-' => '',
    'N/A' => 'SIN CEDULA',
    "''" => 'SIN CEDULA'
    );

    $valoresReemplazoCarnet = array(
    '-' => '',
    'N/A' => 'SIN CARNET',
    "''" => 'SIN CARNET'
    );

    $valoresReemplazoEmail = array(
    '-' => '',
    'N/A' => 'SIN EMAIL',
    "''" => 'SIN EMAIL'
    );

    $valoresReemplazoTelefono = array(
    '-' => '',
    ' ' => '',
    'N/A' => 'SIN TELEFONO',
    "''" => 'SIN TELEFONO'
    );

    $valoresReemplazoDominio = array(
    '-' => '',
    'N/A' => 'SIN DOMINIO',
    "''" => 'SIN DOMINIO'
    );

    $valoresReemplazoNombre = array(
    '-' => '',
    'N/A' => 'SIN NOMBRE',
    "''" => 'SIN NOMBRE'
    );

    $valoresReemplazoObservacion = array(
    'N/A' => 'SIN OBSERVACIONES',
    "''" => 'SIN OBSERVACIONES'
    );

    $valoresReemplazoDireccion = array(
    'N/A' => 'SIN DIRECCION',
    "''" => 'SIN DIRECCION'
    );

    $valoresReemplazoOPEN = array(
    '-' => '',
    'N/A' => 'SIN OPEN',
    "''" => 'SIN OPEN'
    );

    $valoresReemplazoODA = array(
    '-' => '',
    'N/A' => 'SIN ODA',
    "''" => 'SIN ODA'
    );

    $valoresReemplazoWC = array(
    '-' => '',
    'N/A' => 'SIN WEBCLIENT',
    "''" => 'SIN WEBCLIENT'
    );

    $valoresReemplazoSIV = array(
    '-' => '',
    'N/A' => 'SIN SIV',
    "''" => 'SIN SIV'
    );

    $valoresReemplazoQFLOW = array(
    '-' => '',
    'N/A' => 'SIN QFLOW',
    "''" => 'SIN QFLOW'
    );

     $this->load->model('EmpleadoModel');
     // $fechaingresos = $fechaingreso->format('d/m/Y');
     // $fechaingresos = $this->input->post("fecha_ingreso");
     //$fechaingreso= DateTime::createFromFormat('Y-m-d',$this->input->post("date"));
      //$fechaingreso= DateTime::createFromFormat('Y-m-d',$this->input->post("fecha_ingreso"))->format('d/m/Y');
      //echo Datetime::createFromFormat('Y-m-d', $this->input->post("fecha_ingreso"))->format('d/m/Y');
     $fechaingreso=$this->input->post("fecha_ingreso");
     $time = strtotime($fechaingreso);
     $fecha = date('d/m/Y',$time);
     $direcciones=$this->reemplazarCadenaAsoc($valoresReemplazoDireccion, preg_replace('/\s+/', ' ',ltrim($this->input->post("direccion"))));
     $nombre=$this->reemplazarCadenaAsoc($valoresReemplazoNombre, preg_replace('/\s+/', ' ',ltrim($this->input->post("nombrecompleto"))));
     $cedula=$this->reemplazarCadenaAsoc($valoresReemplazoCedula, preg_replace('/\s+/', '',ltrim($this->input->post("cedula"))));
     $carnet=$this->reemplazarCadenaAsoc($valoresReemplazoCarnet, preg_replace('/\s+/', '',ltrim($this->input->post("carnet"))));
     $telefono=$this->reemplazarCadenaAsoc($valoresReemplazoTelefono, preg_replace('/\s+/', ' ',ltrim($this->input->post("telefono"))));
     $observaciones=$this->reemplazarCadenaAsoc($valoresReemplazoObservacion, preg_replace('/\s+/', ' ',ltrim($this->input->post("observaciones"))));
     $email=$this->reemplazarCadenaAsoc($valoresReemplazoEmail, preg_replace('/\s+/', ' ',ltrim($this->input->post("email"))));
     $dominios=$this->reemplazarCadenaAsoc($valoresReemplazoDominio, preg_replace('/\s+/', '',ltrim($this->input->post("dominioempleado"))));
     $usr_open=$this->reemplazarCadenaAsoc($valoresReemplazoOPEN, preg_replace('/\s+/', '',ltrim($this->input->post("usr_open"))));
     $usr_oda=$this->reemplazarCadenaAsoc($valoresReemplazoODA, preg_replace('/\s+/', '',ltrim($this->input->post("usr_oda"))));
     $usr_qflow=$this->reemplazarCadenaAsoc($valoresReemplazoQFLOW, preg_replace('/\s+/', '',ltrim($this->input->post("usr_qflow"))));
     $usr_webclient=$this->reemplazarCadenaAsoc($valoresReemplazoWC, preg_replace('/\s+/', '',ltrim($this->input->post("usr_webclient"))));
     $siv=$this->reemplazarCadenaAsoc($valoresReemplazoSIV, preg_replace('/\s+/', '',ltrim($this->input->post("usr_siv"))));
     $flag=$this->EmpleadoModel->existe_empleado($carnet,$cedula);
     if ($flag === true) {
       echo '<script>alert("El numero de carnet ya se encuentra registrado.")</script>';
       return;
  }
  $fechaingreso = DateTime::createFromFormat('Y-m-d', $this->input->post("fecha_ingreso"));

  if (empty($carnet)) {
    $carnet = $this->generarAleatorio(7);
  }
  if( empty($direcciones)){
      $direcciones = 'Sin Direccion';
  }
  if( empty($observaciones)){
      $observaciones = 'Sin Observaciones';
  }
  if( empty($telefono) || $telefono == '0'){
    $telefono = 'Sin Telefono';
  }
  if( empty($dominios) || strcasecmp($dominios, 'No tiene') == 0){
      $dominios = 'Sin Dominio';
  }
  if( empty($cedula)){
    $cedula = 'Sin Cedula';
  }
        $save = array('idtienda'=>$this->input->post("tienda"),'idtipoempresa'=>$this->input->post("empresa"),'idfuncion'=>$this->input->post("funcion"),'cedula'=>strtoupper($cedula),'carnetempleado'=>strtoupper($carnet),
    'rutafoto'=>("uploads/default-user.png"),'nombrecompleto'=>strtoupper($nombre),'email'=>strtoupper($email),'telefono'=>strtoupper($telefono),'direccion'=>strtoupper($direcciones),
    'fecha_ingreso'=>$fecha,'observaciones'=>strtoupper($observaciones),'estado'=>(true),'dominioempleado'=>strtoupper($dominios));
    $idEmpleado = $this->EmpleadoModel->save($save);
    if ($idEmpleado != 0) {
      $this->load->model('USRModel');
      if( empty($usr_open) || strcasecmp($usr_open, 'No tiene')==0){
          $usr_open = 'Sin OPEN';
        }
        if( empty($usr_oda) || strcasecmp($usr_oda, 'No tiene')==0){
            $usr_oda = 'Sin ODA';
          }
          if( empty($usr_webclient) || strcasecmp($usr_webclient, 'No tiene')==0){
              $usr_webclient = 'Sin WEBCLIENT';
            }
            if( empty($usr_qflow) || strcasecmp($usr_qflow, 'No tiene')==0){
                $usr_qflow = 'Sin QFLOW';
              }
              if( empty($siv) || strcasecmp($siv, 'No tiene')==0){
                  $siv = 'Sin SIV';
                }
      $usr = array('idempleado'=>$idEmmpleado,'usr_open'=>strtoupper($usr_open),'usr_oda'=>strtoupper($usr_oda),'usr_webclient'=>strtoupper($usr_webclient),'usr_qflow'=>strtoupper($usr_qflow),'siv'=>strtoupper($siv));
      $idUSR = $this->USRModel->save($usr);
      if ($idUSR != 0) {
        echo '<script>alert("Se han registrado todos los datos")</script>';
        return false;
      }else {
        return true;
      }
    }else {
      return false;
    }
    }

以及这样可以保存值但仍然显示错误的结果

enter image description here

1 个答案:

答案 0 :(得分:0)

解决方案很有趣,我更改了表单帖子的ajax,并且值完美地发送:)

<?php echo form_open('empleadoController/save_empleado', $create_form); ?>