Problem sending controller array to model

2

I have a problem sending a array containing the data of a form from contralodor to modelo , when the data reaches the database arrive NULL , I have reviewed the code, I searched in Internet and I can not find what is wrong ...

CONTROLLER

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Pacientes extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Pacientes_model');

    }

    public function guardar() {
        $datos_paciente = array(
            'cod_tii_paciente' => $this->input->post('sel_ti'),
            'no_ide_paciente' => $this->input->post('text_nro_ide'),
            'nom1_paciente' => $this->input->post('text_nom1'),
            'nom2_paciente' => $this->input->post('text_nom2'),
            'ape1_paciente' => $this->input->post('text_ape1'),
            'ape2_paciente' => $this->input->post('text_ape2'),
            'sexo_paciente' => $this->input->post('sel_sexo'),
            'fec_nac_paciente' => $this->input->post('text_fec_nac'),
            'cod_est_civ_paciente' => $this->input->post('sel_ec'),
            'cod_ocup_paciente' => $this->input->post('text_ocup'));

        $this->Pacientes_model->insertar($datos_paciente);


    }

}

MODEL

<?php

class Pacientes_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }


    public function insertar($datos_paciente){
        $this->db->insert('pacientes',$datos_paciente);
    }


}

THIS IS THE VIEW:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Documento sin título</title>
        <link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/bootstrap.min.css">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    </head>

    <body>
        <div class="container">
            <br>
            <br>
            <br>
            <div class="panel panel-primary">
                <div class="panel-heading">Ingreso de Pacientes</div>
                <div class="panel-body">
                    <form id="form" class="form-horizontal" role="form">

                        <div class=”panel panel-defaul”> 
                             <div class=”row”>  
                                <div class="form-group col-md-6">
                                    <label for="ejemplo_email_3" class="col-lg-5 control-label">Tipo de Identificacion</label>
                                    <div class="col-lg-5">
                                        <select name="sel_ti" class="form-control control-label" id="sel_ti">
                                            <?php
                                            foreach ($consulta_ti as $i) {
                                                echo '<option value="' . $i->cod_ti_ide . '">' . ucwords($i->nom_ti_ide) . '</option>';
                                            }
                                            ?>
                                        </select>
                                    </div>  
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="ejemplo_email_3" class="col-lg-5 control-label">Numero de Indetificacion</label>
                                    <div class="col-lg-5">
                                        <input class="form-control" name="text_nro_ide" id="text_nro_ide">
                                    </div>
                                </div>
                            </div> 
                        </div>

                        <div class=”row”>  
                            <div class="form-group col-md-6">
                                <label for="ejemplo_email_3" class="col-lg-5 control-label" name="nombre">Primer Nombre</label>
                                <div class="col-lg-5">
                                    <input class="form-control"  name="text_nom1" id="text_nom1">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="ejemplo_email_3" class="col-lg-5 control-label">Segundo Nombre</label>
                                <div class="col-lg-5">
                                    <input class="form-control"  name="text_nom2" id="text_nom2">
                                </div>
                            </div>
                        </div> 
                        <div class=”row”>  
                            <div class="form-group col-md-6">
                                <label for="ejemplo_email_3" class="col-lg-5 control-label">Primer Apellido</label>
                                <div class="col-lg-5">
                                    <input class="form-control"  name="text_ape1" id="text_ape1">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="ejemplo_email_3" class="col-lg-5 control-label">Segundo Apellido</label>
                                <div class="col-lg-5">
                                    <input class="form-control"  name="text_ape2" id="text_ape2">
                                </div>
                            </div>
                        </div> 
                        <div class=”row”>  
                            <div class="form-group col-md-6">
                                <label for="ejemplo_email_3" class="col-lg-5 control-label">Sexo</label>
                                <div class="col-lg-5">
                                    <select class="form-control control-label" id="sel_sexo" name="sel_sexo">
                                        <option value="M">Masculino</option>
                                        <option value="F">Femenino</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="ejemplo_email_3" class="col-lg-5 control-label">Fecha de Nacimiento</label>
                                <div class="col-lg-5">
                                    <input  type="date" class="form-control" id="text_fec_nac" name="text_fec_nac">
                                </div>
                            </div>
                        </div> 
                        <div class=”row”>  
                            <div class="form-group col-md-6">
                                <label for="ejemplo_email_3" class="col-lg-5 control-label">Estado Civil</label>
                                <div class="col-lg-5">
                                    <select class="form-control control-label" id="sel_ec" name="sel_ec">
                                        <?php
                                        foreach ($consulta_ec as $i) {
                                            echo '<option value="' . $i->cod_est_civil . '">' . ucwords($i->desc_est_civil) . '</option>';
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="ejemplo_email_3" class="col-lg-5 control-label">Ocupacion</label>
                                <div class="col-lg-5">
                                    <input class="form-control"  name="text_ocup" id="text_ocup">
                                </div>
                            </div>
                        </div> 
                        <div class=”row”>  
                            <div class="form-group col-md-6">
                                <label for="ejemplo_email_3" class="col-lg-5 control-label">Direccion</label>
                                <div class="col-lg-5">
                                    <input class="form-control"  name="text_dir" id="text_dir">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="ejemplo_email_3" class="col-lg-5 control-label">Telefono Fijo</label>
                                <div class="col-lg-5">
                                    <input class="form-control"  name="text_tel" id="text_tel">
                                </div>
                            </div>
                        </div> 
                     <div class="row">
                            <div class="col-sm-offset-5 col-sm-2 text-center">
                                <div class="btn-group" data-toggle="buttons">
                                    <button type="button" id="btn_enviar" class="btn btn-primary">Enviar</button>
                                </div>
                            </div>
                        </div>
                </div>
                </form>
            </div>
        </div>
    </div>

    <script src="<?php echo base_url(); ?>assets/js/jquery-1.11.3.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/js/pacientes.js"></script>
</body>
</html>

The JS file contains:

$(document).ready(function () {
    $("#btn_enviar").click(function () {
        //   alert ('hola');
        $.ajax({
            url: 'http://localhost/gestiontotweb/pacientes/guardar',
            type: 'POST',
            data: $('#form').serialize(),
            success: function (msj) {
                alert(msj);
                //    $("#mensaje").html(msj);
            }
        });
    });
});

database config:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'sa',
    'password' => '********',
    'database' => 'GESTIONCLINICA',
    'dbdriver' => 'sqlsrv',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);
    
asked by ganedugo 16.09.2016 в 05:16
source

2 answers

0

The problem is that you have not started the connection to the database, when creating your model CodeIgniter does not initialize the connection to the db automatically, so you have to do it manually.

Your model is currently like this:

class Pacientes_model extends CI_Model {

  function __construct() {
    parent::__construct();
  }


  public function insertar($datos_paciente){
    $this->db->insert('pacientes',$datos_paciente);
  }
}

To initialize the connection to the database you should have something like this

class Pacientes_model extends CI_Model {

  function __construct() {
    parent::__construct();
    $this->load->database(); //Aquí inicializas la conexión
  }


  public function insertar($datos_paciente){
    $this->db->insert('pacientes',$datos_paciente);
  }
}

Assuming that the database configuration is correct should save you correctly.

Something additional is that you should also check with an if you insert data correctly, because if you leave it like this you will not have a way of knowing that I return the insert, for this you could do something like this:

  public function insertar($datos_paciente){
    if($this->db->insert('pacientes',$datos_paciente)){
      return $this->db->insert_id(); //Para obtener el ultimo id insertado de la tabla pacientes
    }else{
      return $this->db->error();
    }
  }

On your ajax call try to do the following:

$.ajax({
         url: 'http://localhost/gestiontotweb/pacientes/guardar',
         type: "POST",
         dataType: 'json',
         data: { 'sel_ti': $('sel_ti').val(),
                 'text_nro_ide': $('text_nro_ide').val()
               },
         success: function (msj) {
            alert(msj);
            //    $("#mensaje").html(msj);
         }
       });

Then make a print_r ($ patient_data); in your controller to see if the information is coming through ajax.

    
answered by 16.09.2016 в 06:14
0

Review by steps. In your ajax add

beforeSend: function () {console.log ($ ('# form'). serialize ());},

so you can visualize if you are sending data. (Personally, I am very much trouble with the form.serialize (); )

When you get to your controller before sending to the DB, you have a print_r ($ patient_data);

and cachalos en en
success: function (data) {console.log (data);},
So you check that you are sending data and receiving.

Before saving in the db, ie the model you have a return of your array so you can check that you are sending data and print them with a var_dump ($ array);

With this function you know what kind of varaibles you are going to save. Activate the $ this- > output- > enable_profiler (TRUE); CI So you see the insert and visualize what type of variables you are entering in what type of field.

Usually I fail to send the data in the ajax or to enter the data in the db, for the configuration of the table, if you have data as not null and do not enter anything does not keep the record you do not give the format suitable to date

    
answered by 10.10.2016 в 21:01