There are similar questions already on here, but none of them quite suit my situation. Basically, I want to populate a variable number of select menus with the same data from a MYSQL database. The reason for this is that I would like the user to be able to add rows to submit multiple choices simultaneously.
In order to avoid doing a login --> read of MYSQL for each instance of the select menu, I created an array which would be used to populate the menu whenever necessary. The problem is that it works ONLY for the first instance; in other words, PHP is somehow deleting the values stored in the array after the first reading.
I apologise if this question is very basic, I am quite new to PHP.
Here is my code so far:
TO ACCESS MYSQL AND DEFINE VARIABLE 'result':
<?php
$servername = "localhost";
$username = "sonch_PUBLIC";
$password = "sonch_PUBLIC";
$dbname = "sonch_MAIN";
date_default_timezone_set('Europe/Zurich');
$date = date('Y-m-d');
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//START SEARCH BY VENUE
$sql = "SELECT Name, City FROM Venue ORDER BY City";
$result = $conn->query($sql);
$conn->close();
?>
TO POPULATE THE FIRST INSTANCE OF :
<select style="width:138px; text-align:center">
<option selected>CHOOSE VENUE</option>';
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option>" . $row["Name"] . " - " . $row["City"] . "</option>";
}
}
?>
</select>
So far so good.... but when I want to populate subsequent menus with the same code, i.e., by repeating:
<select style="width:138px; text-align:center">
<option selected>CHOOSE VENUE</option>';
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option>" . $row["Name"] . " - " . $row["City"] . "</option>";
}
}
?>
</select>
...they are empty.
Suggestions?