I strongly suggest that you give up the horrible mix of PHP / HTML code. Honestly it is something that should be forbidden.
When you need HTML code inside a PHP block it is better to use a concatenation variable to build all the content in it. You will gain clarity in the code.
Is that you have to break your head well to analyze this and see where the error is:
echo "<option value=".$fila['nombre_escuela']."'";
if($fila['nombre_escuela']== $ni){echo " selected='true'";}
echo">".$fila['nombre_escuela']."</option>";
I propose this solution in which:
- We are going to work all the time inside a PHP block, to avoid that horrible, confusing and unanalizable PHP / HTML mix
- Let's capture the value of the
nombre_escuela
column in a variable
- We are going to use a ternary to add or not
selected
to the option
that should take it. Just put selected
, it is not necessary to put selected=true
. When selected
is omitted it equals false
and when it is set equals true
( see W3C Recommendation about this.)
- We're going to use
fetch_assoc
, because it's more specific that fetch_array
, since this one brings you two results for each column, loading much more memory ( see note at end of this answer ).
This is the code, I think you can see the difference in terms of clarity:
<?php
$html='<select name="escuelaprocedencia" class="form-control">';
$html.='<option value="">- Selecciona -</option>';
$conn = mysql_connect("localhost","radx","sands");
mysql_select_db("softn_cms",$conn);
$consulta_mysql='select * from escuela_proce';
$resultado_consulta_mysql=mysql_query($consulta_mysql,$conn);
while($fila=mysql_fetch_array($resultado_consulta_mysql)){
$nombre=$fila['nombre_escuela'];
$value=( $nombre == $ni ) ? "value=\"$nombre\" selected" : "value=\"$nombre\"";
$html.="<option $value>$nombre</option>";
}
$html.="</select>";
echo $html;
?>