Welcome Guest, Not a member yet? Register   Sign In
Editing an intem in my DB
#1

[eluser]Alejandrito[/eluser]
I´m new to code igniter so I hope you can help me with this.

I have a form to edit an article so I get the data from the DB and set the values of that article so the form is not empty. My question is: how do I use the new/old info to update the DB?

my Controller looks like this:

Code:
function confirm_edit($id)
    {
        $this->form_validation->set_rules('nombre', 'Nombre', 'trim|xss_clean');            
        $this->form_validation->set_rules('codigo', 'Código', 'trim|is_numeric');
        $this->form_validation->set_rules('cod_barras', 'Código de Barras', 'trim');    
        $this->form_validation->set_rules('descripcion', 'Descripción', 'trim');        
        $this->form_validation->set_rules('familia', 'Familia', 'trim');            
        $this->form_validation->set_rules('subfamilia', 'Subfamilia', 'trim');
        $this->form_validation->set_rules('proveedores', 'Proveedores', 'trim');

        $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');


/* NOT SURE ABOUT THIS PART!!   */
        $datos = array(
                'nombre'          => set_value('nombre'),
                'codigo'           => set_value('codigo'),
                'cod_barras'  => set_value('cod_barras'),
                'descripcion'  => set_value('descripcion'),
                'familia'           => set_value('familia'),
                'subfamilia'     => set_value('subfamilia'),
                'proveedor'     => set_value('proveedor')
                );
             if ($datos['articulos']=$this->mod_articulos->editar($id))
            {
                   echo "ok";
            }
    }

This is the function I use in the model:

Code:
function editar($id)
{
$datos = $this->mod_articulos->unArticulo($id);
$editar = array(
             "familia"       => $datos[0]->familia,
             'subfamilia'    => $datos[0]->subfamilia,
             'codigo'        => $datos[0]->codigo,
             'cod_barras'    => $datos[0]->cod_barras,
             'nombre'        => $datos[0]->nombre,
             'descripcion'   => $datos[0]->descripcion,
             'pcp'           => $datos[0]->pcp,
             'pvp'           => $datos[0]->pvp,
             'proveedor'     => $datos[0]->proveedor,
             'stock'         => $datos[0]->stock,
             'minstock'      => $datos[0]->minstock
         );
            
            $this->db->where('id', $datos[0]->id);
            $this->db->update('articulos', $editar);
}
the view looks like this:

Code:
<tr>
            <td><label for="descripcion">Descripci&oacute;n</label></td>
            <td>&lt;input id="descripcion" type="text" name="descripcion"  value="&lt;?php echo set_value('descripcion', $articulo[0]-&gt;descripcion); ?&gt;"  &gt; <br>&lt;?php echo form_error('descripcion'); ?&gt;</td>
        </tr>

but despite everything, the query doesn't reflect the changes made in the DB. I`m always changing the "descripcion" field but this is what the debugger says:

Code:
UPDATE `articulos` SET `familia` = '02', `subfamilia` = '01', `codigo` = '02', `cod_barras` = '13123', `nombre` = 'Guantes de plastico', `descripcion` = '', `pcp` = '5.00', `pvp` = '9.00', `proveedor` = '02', `stock` = '100', `minstock` = '10' WHERE `id` =  '4'

(the item with id=4 exists)

I would be very thankful if any of you can give me a hint on this matter!!
#2

[eluser]tente[/eluser]
How do you handle to get the $id of the db table entry?

And what is this doing?:

Code:
$datos = $this->mod_articulos->unArticulo($id);

Maybe you're charging the previous database data?

What I'd do is setting a hidden input with the id and trying to read it when the form is posted. And then, sell all the data to the model, if the id is false, create a new entry, if not: update.

#3

[eluser]Alejandrito[/eluser]
Hi!

unArticulo($id) gets the info of one item only. I use this to show a single item

When I see a single item, I have an "edit" button, like this:

Code:
<td class="center"><a href="&lt;?php echo site_url('articulos/editar/'.$articulo[0]-&gt;id)?&gt;">Editar</a></td>

This would be the editing view's body:
Code:
&lt;body&gt;
    &lt;?php echo form_open('articulos/confirm_edit/'.$articulo[0]->id); ?&gt;
    <table class="center">
        <thead>
            <tr><th colspan="2" class="center">Editar "&lt;?=$articulo[0]->nombre;?&gt;"</th></tr>
        </thead>
        <tbody>
        <tr>
            <td><label for="nombre">Nombre</label></td>
            <td>&lt;input id="nombre" type="text" name="nombre"  value="&lt;?=$articulo[0]-&gt;nombre?&gt;"&gt;&lt;br>&lt;?=form_error('nombre')?&gt;</td>
        </tr>
        <tr>
            <td><label for="codigo">C&oacute;digo</label></td>
            <td>&lt;input id="codigo" type="text" name="codigo"  value="&lt;?php echo $articulo[0]-&gt;familia; echo $articulo[0]-&gt;subfamilia; echo $articulo[0]-&gt;codigo?&gt;" &gt;&lt;br> &lt;?php echo form_error('codigo'); ?&gt;</td>
        </tr>
        <tr>
            <td><label for="cod_barras">C&oacute;digo de Barras</label></td>
            <td>&lt;input id="cod_barras" type="text" name="cod_barras" value="&lt;?php echo set_value('cod_barras', $articulo[0]-&gt;cod_barras); ?&gt;" &gt;&lt;br> &lt;?php echo form_error('cod_barras'); ?&gt;</td>
        </tr>
        <tr>
            <td><label for="descripcion">Descripci&oacute;n</label></td>
            <td>&lt;input id="descripcion" type="text" name="descripcion"  value="&lt;?php echo set_value('descripcion', $articulo[0]-&gt;descripcion); ?&gt;"  &gt; <br>&lt;?php echo form_error('descripcion'); ?&gt;</td>
        </tr>
        <tr>
            <td><label for="proveedor">Proveedor</label></td>
            <td>&lt;?php echo form_dropdown('proveedor', array('' => 'Proveedor'));?&gt;<br>&lt;?php echo form_error('proveedor'); ?&gt;</td>
        </tr>
        <tr>
            <td><label for="familia">Familias</label></td>
            <td>&lt;?php echo form_dropdown('familia', array('' => 'Familia'));?&gt;<br>&lt;?php echo form_error('familia'); ?&gt;

            </td>
        </tr>
        <tr>
            <td><label for="pvp">Precio de Venta</label></td>
            <td>&lt;input id="pvp" type="text" name="pvp" value="&lt;?php echo set_value('pvp', $articulo[0]-&gt;pvp); ?&gt;" &gt;&lt;/td>

        </tr>
        <tr>
            <td><label for="pcp">Precio de Compra</label></td>
            <td>&lt;input id="pcp" type="text" name="pcp" value="&lt;?php echo set_value('pcp', $articulo[0]-&gt;pcp); ?&gt;" &gt;&lt;/td>

        </tr>
        <tr>
            <td><label for="subfamilia">Subfamilia</label></td>
            <td>&lt;?php echo form_dropdown('subfamilia', array('' => 'Subfamilia')); ?&gt;<br>&lt;?php echo form_error('subfamilia'); ?&gt;</td>
        </tr>
        <tr>
            <td colspan="2" class="center">&lt;?php echo form_submit( 'submit', 'Enviar');?&gt; </td>
        </tr>
    </tbody>
    </table>
&lt;?php echo form_close(); ?&gt;
&lt;/body&gt;

Oh! and I'm sure is note false Smile
#4

[eluser]weboap[/eluser]
read the data for that specific $id
something like
in model
Code:
public function getByID( $id ) {
        
        $id =(int)$id;

       $query = $this->db->where( 'id', $id )->limit( 1 )->get( 'articulos' );

        if( $query->num_rows() > 0 ) {
            return $query->row();
        } else {
            return false;
        }
    }


in controller
method to show data only no validation
Code:
function edit($id){

$data['records'] = $this->your_model->getByID($id);

$this->load->view('my_fom', $data);


}


in the form view

Code:
&lt;?php if(!$records){

echo "unavailable data!";

else{
?&gt;

.....
//INPORTANT use a hidden input for ID
//your form action to go to validation method example : site_url('yourcontroller/confirm_edit');
//your form with set_value( $records->field_name ) for each input

.....



&lt;? }?&gt;


in the confirm_edit
Code:
function confirm_edit()
    {
        $this->form_validation->set_rules('nombre', 'Nombre', 'trim|xss_clean');            
        $this->form_validation->set_rules('codigo', 'C&oacute;digo', 'trim|is_numeric');
        $this->form_validation->set_rules('cod_barras', 'C&oacute;digo de Barras', 'trim');    
        $this->form_validation->set_rules('descripcion', 'Descripci&oacute;n', 'trim');        
        $this->form_validation->set_rules('familia', 'Familia', 'trim');            
        $this->form_validation->set_rules('subfamilia', 'Subfamilia', 'trim');
        $this->form_validation->set_rules('proveedores', 'Proveedores', 'trim');

        $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');


if ($this->form_validation->run() == FALSE)
  {
   $this->load->view('my_form');
  }
  else
  {
  
                $data = array(
                          'id'=>$this->input->post('id'),
                          'nombre'=>$this->input->post('nombre'),
                          'code'=>$this->input->post('code'),
                           .........

                           );

                     $updated=$this->mod_articulos->editar($data)


                if ($updated)
            {
                   echo "ok";
            }else{
                   echo "error";
             }
                

              
  }



in your model again now to save the data

Code:
function editar($data){

           $this->db->where('id', $data['id']);
           $updated = $this->db->update('articulos', $data);

         return $updated;


}




the above need some correction of names and more work but you get the logic!





Theme © iAndrew 2016 - Forum software by © MyBB