Dan Dan - 7 months ago 22
SQL Question

PHP/MySQL: Values in a select box associate to values in associative array

I have a dropdown list (HTML select box) which gets values from this MySQL query:


"SELECT cdID, cdTitle FROM CD ORDER BY cdID"


The result is then stored in an associative array, which is then output to the dropdown list:

<?php
echo '<select name= "list" id="list">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['cdTitle'].'">'.$row['cdTitle'].'</option>';
}
echo '</select>';
?>


My issue is that I would like the user to see the title of the CD, but for the actual value to be "cdID" as that is the foreign key used in my database.

Answer

Just change the attribute echoed out for value -

<?php
echo '<select name= "list" id="list">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['cdID'].'">'.$row['cdTitle'].'</option>';
}
echo '</select>';
?>  
Comments