I can't edit records in the table

Issue

I have a table and I haven’t been able to edit the values! It only saves me the editing values ​​of the last row, but not the others 🙁

The ID is "NroContacto" and the value I am trying to edit is "estadoActa"

this is my table (view):

enter image description here

<table class="table table-striped">
                      <thead style=background-color:#663399;>
                        <tr>
                            <th style=color:white;>Número Ticket</th>
                            <th style=color:white;>Fecha</th>
                            <th style=color:white;>Motivo</th>
                            <th style=color:white;>Comuna</th>
                            <th style=color:white;>Region</th>
                            <th style=color:white;>Fecha Creación Acta</th>
                            <th style=color:white;>Estado Ticket</th>
                            <th style=color:white;>Fecha Real Cumplimiento</th>
                            <th style=color:white;>Acciones</th>
                            <th style=color:white;>Historial</th>


                        </tr>
                      </thead>
                     <tbody id="myTable">
                         <?php if ($actascreadas): ?>
                            <?php $i = 1;?>
                            <?php foreach ($actascreadas as $act): ?>  
                                                 <form method="post" action="<?= base_url() ?>index.php/administradoractas/update" > 

                        <tr>  
                            <td> <a href="<?php echo base_url(); ?>index.php/creacionacta/edit/<?php echo $act->NroContacto; ?>" target="_blank"><?php echo $act->NroContacto; ?></a> <input type=hidden name="NroContacto" value="<?php echo $act->NroContacto; ?>" /> </td>
                            <td><?php echo $act->fechaGeneracion; ?></td>
                            <td><?php echo $act->tipoGestion; ?></td>
                            <td><?php echo $act->Comuna1; ?></td>
                             <td><?php echo $act->estadoActa; ?></td>
                            <td><?php echo $act->fechaGeneracion; ?></td>
                            <td>  
                                <select  id="slct" style=width:100px;align:left;border-color:#6A148E; name="estadoActa" placeholer="Seleciona una opcion">
                                    <optgroup label="Ingresado"> 
<option <?php if($act->estadoActa == "No Tomado"){ echo 'selected="selected"'; } ?> value="No Tomado">No Tomado</option>                                        
<option <?php if($act->estadoActa == "Ingresado - Ingresado"){ echo 'selected="selected"'; } ?> value="Ingresado - Ingresado">Ingresado - Ingresado</option>
                                     <optgroup label="Realizado"> 
<option <?php if($act->estadoActa == "Realizado - Retiro Realizado"){ echo 'selected="selected"'; } ?> value="Realizado - Retiro Realizado">Realizado - Retiro Realizado</option>
                                         
<option <?php if($act->estadoActa == "Realizado - Cambio Realizado"){ echo 'selected="selected"'; } ?> value="Realizado - Cambio Realizado">Realizado - Cambio Realizado</option>
                                         <optgroup label="Incompleto">
  <option <?php if($act->estadoActa == "Incompleto - Capacidad del Camion"){ echo 'selected="selected"'; } ?> value="Incompleto - Capacidad del Camion">Incompleto - Capacidad del Camion</option>     

 <option <?php if($act->estadoActa == "Incompleto - Retiro Incompleto"){ echo 'selected="selected"'; } ?> value="Incompleto - Retiro Incompleto">Incompleto - Retiro Incompleto</option>  
                                    <optgroup label="Incumplimiento">
 <option <?php if($act->estadoActa == "Incumplimiento - Sin Moradores"){ echo 'selected="selected"'; } ?> value="Incumplimiento - Sin Moradores">Incumplimiento - Sin Moradores</option>
                                        
<option <?php if($act->estadoActa == "Incumplimiento - Cliente Desiste"){ echo 'selected="selected"'; } ?> value="Incumplimiento - Cliente Desiste">Incumplimiento - Cliente Desiste</option> 
                                        
<option <?php if($act->estadoActa == "Incumplimiento - Capacidad del Camion"){ echo 'selected="selected"'; } ?> value="Incumplimiento - Capacidad del Camion">Incumplimiento - Capacidad del Camion</option> 
                                        
                    </select>
                                
                            </td>
                                                         <td><input id="date" name="fechaRealCumplimiento" type="date" value="<?php echo $act->fechaRealCumplimiento; ?>"></td>

                            <td>

        <input type="submit" name="actualizar[]"  class="btn btn-primary float-center" value="Guardar">

                            </td>
                              <td>

<a href="<?php echo base_url(); ?>index.php/administradoractas/historialdeticketpornumeroticket/?NroContacto=<?php echo $act->NroContacto; ?>" target="_blank"><?php echo $act->NroContacto; ?></a>
                            </td>
                        
                        </tr>
                         
                                   
              <?php $i++;?>
                            <?php endforeach;?>
                                    <?php endif;?>   
                   </form>
                      </tbody>
                         
                    </table>

The controller:

 public function update()
    {
    
           
           $data = array(
        'table_name' => 'actas_creadas', // pass the real table name
        'NroContacto' => $this->input->post('NroContacto'),
        'estadoActa' => $this->input->post('estadoActa')
    );

    $this->load->model('Creacionacta_model'); // load the model first
    if($this->Creacionacta_model->update3($data)) // call the method from the model
    {
        // update successful
    }
    else
    {
        // update not successful
    }
            
           redirect("index.php/administradoractas");
            
        
    }

The model:

  public function update3($data) {
    $db3 = $this->load->database('db3', TRUE);
    extract($data);
    $db3->where('NroContacto', $NroContacto);
    $db3->update($table_name, array('estadoActa' => $estadoActa));
    return true;
}

Solution

The simple answer is that you are not closing the form () within the loop. So it is one long form with the last row being what is updated.

<?php foreach ($actascreadas as $act): ?>  
<form method="post" action="<?= base_url() ?>index.php/administradoractas/update" > 
...
...
<?php endforeach;?>
<?php endif;?>   
</form>

Change it so the form starts and ends within the loop

<?php foreach ($actascreadas as $act): ?>  
<form method="post" action="<?= base_url() ?>index.php/administradoractas/update" > 
...
</form>
<?php endforeach;?>

A few other things to consider

  • Separating your HTML and PHP
  • Don’t use array() syntax, it is superseded by [] in later versions of PHP
  • Don’t have a generic update() function. It is very dangerous if values can be injected into you code any table can be updated. Use specific functions with a purpose and a prepared SQL statement
  • Don’t use extract(), it hides what your code is doing relying on magic. Create specific functions for updates with Typed parameters

A good place for best practices is PHP The Right Way

Answered By – Nigel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published