I can not insert in BD with CodeIgniter

1

I have a view with a Modal PopUp form when the accept button is executed, it does not insert in the database or mark any error, I hope you can help me.

VIEW:

    <!-- Modal (PopUp registro de nuevo cliente) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Nuevo Cliente</h4>
            </div>
            <div class="modal-body">

                <form class="form-horizontal" role="form">
                <?php echo form_open('cCliente/Insertar'); ?>
                    <div class="form-group">
                        <label for="TF_Nombre" class="col-lg-2 control-label">Nombre</label>
                        <div class="col-lg-8">
                          <input class="form-control" id="TF_Nombre" name="TF_Nombre">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="TF_RFC1" class="col-lg-2 control-label">RFC</label>
                        <div class="col-lg-5">
                          <input class="form-control" id="TF_RFC1" name="TF_RFC1">
                        </div>
                    </div>
                    <div class="form-group">

                        <button type="submit" class="btn btn-primary" id="Ok" name="Ok">Agregar</button>                                      
                    </div>
                    <?php echo form_close(); ?>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>

            </div> 

CONTROLLER

    <?php

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

class cCliente extends CI_Controller 
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('mente');
    }

    public function index()
    {
        $data = array('titulo' => 'Factura');
        $this->load->view('Plantillas/head', $data);


        $this->load->view('Plantillas/nav');
        $this->load->view('Vistas/vFactura');
        $this->load->view('Plantillas/footer');
    }

    public function Insertar()
    {
        $this->form_validation->set_rules('TF_Nombre','Nombre','required|trim|xss_clean')
        $this->form_validation->set_rules('TF_RFC1','RFC','required|trim|xss_clean')

        if($this->form_validation->run()==FALSE)
        {
            $this->index();
        }
        else
        {
            $TF_Nombre        = $this->input->post('TF_Nombre');
            $RFC                = $this->input->post('TF_RFC1');

            $insert = $this->MCliente->inserta_Cliente($TF_Nombre,$RFC);
        }
    }
}
?>

MODEL:

    <?php

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

class MCliente extends CI_Model 
{
    public function construct()
    {
        parent::__construct();
    }

    public function inserta_Cliente($TF_Nombre,$RFC)
    {
        $data = array('NOMBRE' => $TF_Nombre,
                        'RFC'   => $RFC);
        $this->db->insert('clientes',$data);
    }
}

?>

Thank you in advance. Greetings.

    
asked by Omar Paniagua 25.05.2016 в 02:21
source

3 answers

0

If you use codeigniter 3, your controller cCliente should be called CCient because the format for declaring controllers is to declare the class always with an initial capital letter, check the official documentation Codeigniter 3 .

When inserting, check if the query was executed correctly, you could do something like that in your MCliente model:

public function inserta_Cliente($TF_Nombre,$RFC)
{
    $data = array('NOMBRE' => $TF_Nombre,
                    'RFC'   => $RFC);
    return $this->db->insert('clientes',$data);

}

With that you are returning TRUE or FALSE according to how the insert was executed if it is not executed, returns FALSE.

On your controller to check if it is inserted you could print the value of $ insert and see if it returns TRUE or FALSE.

    
answered by 25.05.2016 в 03:41
0

I think the problem is that you are importing a "mind" model

$this->load->model('mente');

and when you try to execute that model you are calling the model "mCliente"

$insert = $this->MCliente->inserta_Cliente($TF_Nombre,$RFC);

Try to change "mind" with "MCliente"

    
answered by 24.07.2016 в 20:26
0

I think the target driver is missing the form with action:

<form class="form-horizontal" action="controladorDestino.php" role="form">
</form>
    
answered by 03.11.2017 в 21:11