registration with optional fields codeigniter

0

name surname *user *password * email

I have a record with optional fields, but when I insert an insert into the database it gives me an error because I'm doing an insert of the type:

INSERT INTO 'usuario'('nombre', 'apellido', 'usuario', 'contraseña', 'email') VALUES (,, 'usuario', 'contraseña', 'email')

Haber if anyone knows a solution.

    
asked by Doe 10.08.2017 в 16:03
source

2 answers

0

I recommend that you use the properties offered by the framework

CONTROLLER

function post_agregar()
{
   $nombre     = $this->input->post('nombre');
   $apellido   = $this->input->post('apellido');
   $usuario    = $this->input->post('usuario');
   $contrasena = $this->input->post('contrasena');
   $email      = $this->input->post('email');

   $data = array
   (
      'nombre'      => $nombre,
      'apellido'        => $apellido,
      'usuario'     => $usuario,
      'contrasena'  => $contrasena,
      'email'           => $email
   );

$add = $this->tu_modelo->nombre_de_tu_funcion($data);

if ($add == true) 
{
    $this->session->set_flashdata('add','Se registro con exito!');
    redirect('tu_controlador', 'refresh');

}else {

    $this->session->set_flashdata('error','No se ha podido guardar');
    redirect('tu_controlador', 'refresh');
}

}

MODEL

function nombre_de_tu_funcion($data = array())
{
    $this->db->insert('tu_tabla', $data);
    return $this->db->insert_id();
}

With this you enter your data without problems.

NOTE: In programming you can not use the "Ñ"

Greetings.

    
answered by 10.08.2017 / 16:19
source
0

The problem is that you can not write several commas in a row without content between them. If the fields are optional, that is to say they accept null as value, you must specify it if you are going to do the query as you have written it, it would look something like this:

INSERT INTO 'usuario'('nombre', 'apellido', 'usuario', 'contraseña', 'email') VALUES (null,null, 'usuario', 'contraseña', 'email')
    
answered by 10.08.2017 в 17:35