我无法编辑 table 中的记录

I can't edit records in the table

我有一个 table,但我无法编辑这些值!它只保存了最后一行的编辑值,而没有保存其他的:(

ID 是“NroContacto”,我要编辑的值是“estadoActa”

这是我的table(视图):

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

控制器:

 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");
            
        
    }

型号:

  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;
}

简单的回答是您没有在循环内关闭 form ()。所以这是一个长表格,最后一行是更新的内容。

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

更改它,使表单在循环中开始和结束

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

其他一些需要考虑的事情

  • 将您的 HTML 和 PHP
  • 分开
  • 不要使用 array() 语法,它在 PHP
  • 的更高版本中被 [] 取代
  • 没有通用的 update() 函数。如果可以将值注入到您的代码中,那么任何 table 都可以更新,这是非常危险的。使用具有目的和准备好的 SQL 语句
  • 的特定函数
  • 不要使用 extract(),它会隐藏您的代码依赖魔法所做的事情。使用类型化参数
  • 为更新创建特定函数

最佳实践的好地方是 PHP The Right Way