I have made input select tab using mysql database.
Using While:
<select name="membership_ID" id="membership" tabindex="1" required="required">
<option value="">-- Membership Type --</option>
<?php $get_membership = mysql_query("SELECT * FROM membership");
while($row_membership = mysql_fetch_array($get_membership, MYSQL_ASSOC)): ?>
<option value="<?php echo $row_membership['ID']; ?>"> <?php echo $row_membership['name']; ?> </option>
<?php endwhile; ?>
</select>
using For loop:
<?php $get_membership = mysql_query("SELECT * FROM membership");
$numRows = mysql_num_rows($get_membership);
for($i=1; $i<= $numRows; $i++){
$row_membership = mysql_fetch_array($get_membership, MYSQL_ASSOC) ?>
<option value="<?php echo $row_membership['membership_ID']; ?>"> <?php echo $row_membership['membership_name']." - ".$row_membership['membership_price']." Rs"; ?> </option>
<?php } ?>
this works fine and gives values placed as options when no while loop is there. But when while/for loop places, options in while/for loop are not displayed. Note: table membership contains data.
mysql_*
methods as they are deprecated, usemysqli
orPDO
instead. Second you are doing nothing in your code to handle any error conditions from the database. Add this so that you can clearly understand what errors you might be getting. Third, you should probably do a loop earlier in you code to where you don't have this spaghetti code. Why even try to output aselect
element at all if your query failed? – Mike Brant Jan 29 at 0:36