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>
4
  • Loop through the query results, echoing <option> tags containing $tow['clubName'] Commented Jun 5, 2021 at 0:02
  • There are many tutorials on creating dropdown menus from SQL query results. Commented Jun 5, 2021 at 0:02
  • I have tried following quite a few but still not able to get it Commented Jun 5, 2021 at 0: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 phpdelusions.net/pdo & websitebeaver.com/… Commented Jun 5, 2021 at 12:18

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.