Error with query Select Option in database

0

The select does not work with a value that I want to appear from the database.

I have doubts about my code, what I want to do is that the value of a record appears as a value first in the select.

My code:

<?php
 include ("../../conexion/conectar.php");


$id=$_POST['folio'];

  $consultauniversidades= mysql_query("SELECT   iduniversidad, nombreuni FROM universidades",   $con);
$consultaalumno=mysql_query("SELECT iduniversidad   from alumnos where folio='$id'");
$row_consulta= mysql_fetch_assoc    ($consultauniversidades);
$row_alumno= mysql_fetch_assoc($consultaalumno);

     ?>
<select class="form-control" name="Universidad"     id="Universidad" required>         
<?php
      do{ 
        if($row_consulta['iduniversidad']==$row_alumno  ['iduniversidad'] )
        { ?> 
           <option   value ="<?php echo $row_consulta   ['iduniversidad']?>" selected>
          <?php echo $row_consulta['nombreuni'];?>  
        </option>
          <?php  
        } else { 
        ?>
<option value ="<?php echo $row_consulta    ['iduniversidad']?>">
          <?php echo $row_consulta['nombreuni'];?>  
        </option>

        <?php
        }
      }while ($row_consulta=mysql_fetch_assoc   ($consultauniversidades)); 
        ?>               
        </select>          
    
asked by ERICK NOE LOPEZ OCAMPO 10.11.2018 в 05:40
source

3 answers

1

Here I have the modal window and the bottom of the script that is how I charge the select option to the modal window

              <!--Comienza tabla modal para editar-->
                        <div class="container">
                <div id="tablaAlumnos"></div>
      </div>

    <!-- Modal para edicion de datos -->
   <div class="modal fade" id="modalEdicion" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Actualizar datos</h4>
      </div>
       <div class="modal-body">
          <label>Folio</label>
          <input type="text" name="" id="folio" class="form-control input-sm" readonly="readonly">
          <label>Nombre</label>
          <input type="text" name="" id="nom" class="form-control input-sm" placeholder="Nombre..">
          <label>Apellido Paterno</label>
          <input type="text" name="" id="apellidopaterno" class="form-control input-sm" placeholder="Apellido Paterno..." >
          <label>Apellido Materno</label>
          <input type="text" name="" id="apellidomaterno" class="form-control input-sm" placeholder="Apellido Materno...">
          <label>Matricula</label>
          <input type="text" name="" id="matricula" class="form-control input-sm" placeholder="Matricula...">
            <div><label for="Universidad">Universidad: </label> </div>
        <div class="col-md-8" id="universidades">        
           
        </div>
<br>
<br>
      <label>Tipo estancia: </label>      <br>
       <div class="btn-group btn-group-toggle" data-toggle="buttons" id="buttons">
          <label class="btn btn-primary" for="option1">
            <input type="radio" name="options" id="option1" autocomplete="off" value ="Estancia I" checked> Estancia I
          </label>
          <label class="btn btn-primary" for = "option2">
            <input type="radio" name="options" id="option2" autocomplete="off" value ="Estancia II" > Estancia II
          </label>
          <label class="btn btn-primary" for = "option3">
            <input type="radio" name="options" id="option3" autocomplete="off" value ="Servicio Social" > Servicio Social
          </label>
          <label class="btn btn-primary" for ="option4">
            <input type="radio" name="options" id="option4" autocomplete="off" value ="Estadia" > Estadia
          </label>
      </div>   
      <br>
          <label>Grado</label>
          <input type="text" name="" id="grado" class="form-control input-sm" placeholder="Grado...">
          <label>CURP</label>
          <input type="text" name="" id="CURP" class="form-control input-sm" placeholder="CURP...">
          <label>Correo institucional</label>
          <input type="text" name="" id="correoi" class="form-control input-sm" placeholder="Correo institucional...">
          <label>Correo Personal</label>
          <input type="text" name="" id="correop" class="form-control input-sm" placeholder="Correo Personal...">
          <label>Calle</label>
          <input type="text" name="" id="calle" class="form-control input-sm" placeholder="Calle...">
          <label>Colonia</label>
          <input type="text" name="" id="colonia" class="form-control input-sm" placeholder="Colonia...">
          <label>Municipio</label>
          <input type="text" name="" id="municipio" class="form-control input-sm" placeholder="Municipio...">
          <label>Codigo Postal</label>
          <input type="text" name="" id="codigopostal" class="form-control input-sm" placeholder="Codigo Postal...">
          <label>Estado</label>
          <input type="text" name="" id="estado" class="form-control input-sm" placeholder="Estado...">
          <label>NSS</label>
          <input type="text" name="" id="nss" class="form-control input-sm" placeholder="NSS...">
          <label>Numero</label>          
          <input type="text" name="" id="numero" class="form-control input-sm" placeholder="Numero...">
          <label>Estado</label>
          <select class="form-control"  id="activo" required>         
            <option value ="1">Activo</option>        
            <option value ="0">Inactivo</option>        
          </select>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-warning" id="actualizadatos" data-dismiss="modal" >Actualizar
        </button>
        
      </div>
    </div>
  </div>
</div>
<!-- Termina-->                                                                                       
  <script type="text/javascript">
 $(document).ready(function(){
         $('#universidades').load('PHP/listaUniversidades.php');
 }); 
       </script>

 
    
answered by 10.11.2018 в 21:51
0

Try this and let me know if it works, the first while puts the same id in first and selected, and the second while puts the rest of the info in the select ...... see how it works

<?php
// NO COPIAR ESTO ----------------------------->>>>>>>>>>>>>>>>>>>>>
$servidor="localhost";
$usuario="root";
$password="";
$basededatos="stackover";
//Conexion DB
$con = mysql_connect($servidor,$usuario,$password);
mysql_select_db($basededatos,$con);
mysql_query("SET NAMES 'utf8'");
// NO COPIAR ESTO ----------------------------->>>>>>>>>>>>>>>>>>>>>
//$id="1";
// COPIAR DESDE AQUI ABAJO ----------------------------->>>>>>>>>>>>>>>>>>>>>

$id=$_POST['folio'];
$consultauniversidades= mysql_query("SELECT iduniversidad, nombreuni FROM universidades",$con);
$consultaalumno=mysql_query("SELECT iduniversidad from alumnos where folio='".$id."'",$con);
$row_consulta= mysql_fetch_assoc($consultauniversidades);
$row_alumno= mysql_fetch_assoc($consultaalumno);
?>
<select class="form-control" name="Universidad"     id="Universidad" required>         
<?php
while ($row_consulta=mysql_fetch_assoc ($consultauniversidades)){
if($row_consulta['iduniversidad']==$row_alumno['iduniversidad']){
echo'<option value="'.$row_consulta["iduniversidad"].'" selected>'.$row_consulta["nombreuni"].'</option>';
}}
$consultauniversidades= mysql_query("SELECT iduniversidad, nombreuni FROM universidades",$con);
while ($row_consulta=mysql_fetch_assoc ($consultauniversidades)){
if($row_consulta['iduniversidad']!=$row_alumno['iduniversidad']){
echo'<option value="'.$row_consulta["iduniversidad"].'">'.$row_consulta["nombreuni"].'</option>';
}}
?>               
</select> 
    
answered by 10.11.2018 в 17:44
0

What you have to do is simply add the selected attribute to the option when the id matches the one stored in the bd:

...
<select class="form-control" name="Universidad" id="Universidad" required>
    <?php foreach ($row_consulta as $universidad) :
    // sólo si los id coinciden se dibuja el option con el atributo selected
    $selected = ($universidad['iduniversidad'] == $row_alumno['iduniversidad']) ? 'selected' : '';
    ?>
    <option value="<?php echo $universidad['id'];?>" <?php echo $selected;?>><?php echo $universidad['nombreuni'];?></option>
    <?php endforeach; ?>
</select>
...
    
answered by 12.11.2018 в 18:31