0

I'm new to PHP and have created a very basic HTML form. As you can see in my form, the option values are all done by hand (there are more, I just simplified this example). What I want is for these to be generated dynamically using just PHP, so that I would physically have to add every single year etc.

I've done some searching but I can't seem to find exactly what I'm after so thought I'd ask here. From what I gather I need to create a query and echo out the option value somehow, although I'm not sure how to do this.

SELECT gameYear from games

I'd guess the above would be the correct query as all the form would need is the bookYear from the table?

<form id = "gameYear" method="get" action="result.php">
<label> 
    Game Year
    <select name="gameYear">
       <option value="2000">2000</option>
       <option value="2001">2001</option>
       <option value="2002">2002</option>
    </select>
</label>
   <input type = "submit" name="search" value = "Search">
</form>

Thanks, any help/guidance is appreciated.

2 Answers 2

2
<form id = "gameYear" method="get" action="result.php">
<label> 
Game Year
<select name="gameYear">
<option value=''>--Select Year--</option>
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$SqlResult = mysqli_query($link, "SELECT gameYear from games");
while($Row = mysqli_fetch_array($SqlResult))
{
  ?>
  <option value="<?php echo $Row['gameYear'] ?>"><?php echo $Row['gameYear'] ?></option>
}
?>
</select>
</label>
<input type = "submit" name="search" value = "Search">
</form>
Sign up to request clarification or add additional context in comments.

Comments

1
<?php $sql = "SELECT gameYear from games order by gameYear ASC";
    $result = mysql_query($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); ?>


<form id="gameYear" method="get" action="result.php">
<label>Game Year
<select name="gameYear">
<?php while($row = mysql_fetch_array($result)){ ?>                        
<option value="<?php echo $row['gameYear'] ?>"><?php echo $row['gameYear']?></option>
<?php } ?>
</select>
</label>
<input type = "submit" name="search" value = "Search">
</form>

1 Comment

You may also want to add ORDER BY gameYear ASC. Also your <option value="<?php echo $row['gameYear'] ?>"><?php echo $row['fullname']?></option> should be <option value="<?php echo $row['gameYear'] ?>"><?php echo $row['gameYear']?></option>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.