Error inserting data in BD from a form with CodeIgniter



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 class="form-group">
        <label for="apellidos">Apellidos</label>
        <input type="text" class="form-control" id="apellidos" placeholder="Introduce los apellidos">
    <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 class="form-group">
        <label for="nick">Nombre se usuario</label>
        <input type="text" class="form-control" id="nick" placeholder="Introduce el nickname">
    <div class="form-group">
        <label for="pass1">Contraseña</label>
        <input type="password" class="form-control" id="pass1" placeholder="Introduce la contraseña">
    <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">
    <button type="submit" class="btn btn-primary">Crear nuevo usuario</button>

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) {

    } else {
        $fdata=array('danger'=>'Ha ocurrido un error al crear el nuevo usuario');
        foreach ($warning as $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);



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

1 answer


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