php - Data not coming in dropdown box from MySQL -
i trying data mysql table 2 separate dropdown box, first dropdown getting list of cities database, why 2nd dropdown not showing list of cities. here code in php
<?php mysql_connect("localhost", "root", "") or die("connection failed"); mysql_select_db("flywest")or die("connection failed"); $query = "select * cities"; $result = mysql_query($query); ?> depart <select name="formdepart" id="fromdepart"> <?php while ($line = mysql_fetch_array($result, mysql_assoc)) { ?> <option value="<?php echo $line['city_name'];?>"> <?php echo $line['city_name'];?> </option> <?php } ?> </select> </p> arrive <select name="formarrive" id="fromdepart"> <?php while ($line = mysql_fetch_array($result, mysql_assoc)) { ?> <option value="<?php echo $line['city_name'];?>"> <?php echo $line['city_name'];?> </option> <?php } ?> </select> <p> kindly show mistake.
your 1st while loop has reached end of result set , hence running while again starting end , not go further.
you can fetch rows , store in variable , use variable populate both dropdowns.
<?php $cities = array(); while ($line = mysql_fetch_array($result, mysql_assoc)) { $cities[] = $line; } ?> <select name="formdepart" id="fromdepart"> <?php foreach ($cities $line) { ?> <option value="<?php echo $line['city_name'];?>"> <?php echo $line['city_name'];?> </option> <?php } ?> </select> </p> arrive <select name="formarrive" id="fromdepart"> <?php foreach ($cities $line) { ?> <option value="<?php echo $line['city_name'];?>"> <?php echo $line['city_name'];?> </option> <?php } ?> </select> this should work fine.
Comments
Post a Comment