Error inserting data in BD from a form with CodeIgniter

2

Situation:

I have a view with a form to register new users:

<form action="<?php echo base_url('/user/insert_new_user/0'); ?>" method="post">
    <div class="form-group">
        <label for="nombre">Nombre</label>
        <input type="text" class="form-control" id="nombre" placeholder="Introduce el nombre">
    </div>
    <div class="form-group">
        <label for="apellidos">Apellidos</label>
        <input type="text" class="form-control" id="apellidos" placeholder="Introduce los apellidos">
    </div>
    <div class="form-group">
        <label for="mail">Correo electrónico</label>
        <input type="email" class="form-control" id="mail" placeholder="Introduce el correo electrónico">
    </div>
    <div class="form-group">
        <label for="nick">Nombre se usuario</label>
        <input type="text" class="form-control" id="nick" placeholder="Introduce el nickname">
    </div>
    <div class="form-group">
        <label for="pass1">Contraseña</label>
        <input type="password" class="form-control" id="pass1" placeholder="Introduce la contraseña">
    </div>
    <div class="form-group">
        <label for="pass2">Repite la contraseña</label>
        <input type="password" class="form-control" id="pass2" placeholder="Vuelve a escribir la contraseña">
    </div>
    <button type="submit" class="btn btn-primary">Crear nuevo usuario</button>
</form>

The insert_new_user function in the corresponding controller:

public function insert_new_user() {

    if ($this->user_model->insert_new_user($warning)) {
        $fdata=array('success'=>'Usuario creado correctamente');
        foreach ($warning as $warn) {
            $fdata['warning'].=$warn;
        }

    } else {
        $fdata=array('danger'=>'Ha ocurrido un error al crear el nuevo usuario');
        foreach ($warning as $warn) {
            $fdata['danger'].=$warn;
        }
    }

    $this->session->set_flashdata('messages', $fdata);
    redirect(base_url('/user'), 'refresh');
}

And finally the function in the corresponding model:

public function insert_new_user($warnings){

        $f1= $_POST['nombre'];
        $f2= $_POST['apellidos'];
        $f3= $_POST['mail'];
        $f4= $_POST['nick'];
        $f5= $_POST['pass1'];

        $data = array(
                'nombre' => $f1,
                'apellidos' => $f2,
                'email' => $f3,
                'nick' => $f4,
                'pass' => $f5
        );

        $this->db->insert('users', $data);

}

Problem

When you click on the submit of the form, the fields that should arrive by post to the function insert_new_user of the model arrive with values NULL , that is, the SQL query gets executed but gives the error that the values they can not be NULL

What can this be?

    
asked by Hechi 12.08.2016 в 12:30
source

1 answer

1

Try adding the attribute name to each input .

<input type="text" class="form-control" name="nombre" id="nombre"...
    
answered by 12.08.2016 / 12:37
source