0

I have a table called "Registration" for registering into clubs with the following columns 'clubName' and 'registrationStatus' clubName contains the name of clubs and registrationStatus contains one of these three, open, closed, or extended. In my HTML form I have select input. In the options I want to have from the database club names whose status are open and extended only. Or all the names of clubs but those with status closed will not be active. In my code I have the following

<?php 
//connection to database

include "connect.php";
?>




<label for="club">Select club to join</label>
<select name="club" >
<?php
$result = "SELECT clubName FROM Registration WHERE Status= 'Open' OR Status='Extended'";

//I'm stuck here now how to display the options and how will I attach value to them for entry into the database

?>
</select>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Loop through the query results, echoing ` – Barmar Jun 05 '21 at 00:02
  • There are many tutorials on creating dropdown menus from SQL query results. – Barmar Jun 05 '21 at 00:02
  • I have tried following quite a few but still not able to get it – McChris_ Made_It Jun 05 '21 at 00:05
  • If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo & https://websitebeaver.com/php-pdo-prepared-statements-to-prevent-sql-injection – Dharman Jun 05 '21 at 12:18

1 Answers1

2

You would need to first run the query, then cycle through the values using an associative array to get the values. Those values can then be displayed inside your dropdown menu. Be sure to filter your values when you collect them to prevent XSS attacks.

<label for="club">Select club to join</label>
<select name="club" >
<?php
$result = "SELECT clubName FROM Registration WHERE Status= 'Open' OR Status='Extended'";
// be sure do define $connection to be your database connection, or just change it in this code to match your configuration settings
$runquery = mysqli_query($connection,$result);
while($row = mysqli_fetch_assoc($runquery)){
  // this will cycle through the array
  $val = $row['clubName'];
  echo '<option value="' . $val . '">' . $val . '</option>';
}

?>
</select>
Dharman
  • 30,962
  • 25
  • 85
  • 135
dandemo
  • 397
  • 1
  • 5