I have a php 'update' page in which I use an existing database record to load the field data into their respective input fields, to be edited by the user if they wish. Most of these are displayed in textboxes, which works fine including both the HTML and PHP inside a single echo statement like so:
<?php echo"<input type=\"text\" name=\"age\" value=\"{$age}\" class=\"textbox\"><br>"?>
The problem I'm having is trying to use a similar approach when it comes to attempting to use PHP to populate and set initial value in a dropdown box. Here is the associated code:
$query = "SELECT raceID, RaceName FROM races";
$result = $conn->query($query);
<?php echo
"<select value=\"{$racingIn}\">
while ($rowRace = $result->fetch_assoc()){
//add all rows in database collection to dropdown box for selection
<option name=\"racingIn\" value=\"{$rowRace['raceID']}\">{$rowRace['RaceName']}</option>;
}
</select><br>"
?>
It almost works, but I keep getting the error on pageload telling me that the $rowRace
variable is undefined. I know this isn't the case as it is used elsewhere in the program and did work fine until the original 2 separate PHP spans were merged into one. I think it might be something to do with missing {}
brackets around the PHP variables.
Any advice with this, whether it be fixing the existing or suggesting an easier approach, would be massively appreciated.
Thanks, Mark