value of option value is lost when updating

0

friends when I try to update the value of the select does not show me what I had previously registered in the select because the idea is that if you already register a value of the select when there is an update should show you the value you already chose first and then select another value if it were the case.

<?php
$id_mis_cuentas= $_GET['id_mis_cuentas'];
$consulta = $DB_con->prepare("SELECT 
mis_cuentas.id_mis_cuentas,
mis_cuentas.id_bancos_admin,
mis_cuentas.id_cuentas_admin,
mis_cuentas.numero_cuenta_admin,
bancos.id_bancos, 
bancos.bancos,
cuentas.id_cuentas,
cuentas.cuentas
FROM mis_cuentas INNER JOIN bancos ON 
mis_cuentas.id_bancos_admin=bancos.id_bancos
INNER JOIN cuentas ON mis_cuentas.id_cuentas_admin=cuentas.id_cuentas WHERE 
id_mis_cuentas=:id_mis_cuentas");
 $consulta->execute(array(':id_mis_cuentas'=>$id_mis_cuentas));
 $editar_linea = $consulta->fetch(PDO::FETCH_ASSOC);{
 extract($editar_linea);
 ?>
 <form class="col s12 m12 l12" action="actualizarmiscuentas" name="frmContacto" method="GET">

  <div class="input-field col s12 m4">
  <select name="id_cuentas" id="id_cuentas" class="browser-default" required/>
  <option value="0" disabled selected>Tipo de Cuenta:</option>
  <?php
   $consulta = $DB_con->query("SELECT * FROM cuentas ORDER BY id_cuentas");
   while ($linea = $consulta->fetch(PDO::FETCH_ASSOC)) {
    ?>
   <option <?php if($editar_linea['id_cuentas'] == $linea['cuentas']){ ?> selected <?php }?> value="<?= $linea['id_cuentas'] ;?>">
   <?php echo $linea['cuentas'] ;?></option>
  <?php
     }
     ?>
     </select>
     </div>

   <input type='hidden' name='id_mis_cuentas' value='<?php echo $id_mis_cuentas; ?>'>
   </form>
   <?php

    }

   ?>
    
asked by yoclens 13.07.2017 в 14:21
source

1 answer

1

You are doing poorly the comparison of the selected one because in $linea you compare with cuentas and not with id_cuentas .

Change it to this and see if it works.

 <option <?php if($editar_linea['id_cuentas'] == $linea['id_cuentas']){ ?> selected <?php }?> value="<?= $linea['id_cuentas'] ;?>">
    
answered by 13.07.2017 / 14:32
source