How to put table value in a dropdown menu with MYSQL and PHP


Problem :

I want to have a table row from phpmyadmin named: Team in a dropdown menu. So if you have the dropdown menu the menu shows all the teams in a dropdown.

<form action="" method="post">
    <select name="dropdown">
        <option value=""><?php echo $row['Team'] ?></option>
        <option value="">test</option>
    </select>
</form>

This is what i have but when i test it the dropdown doesn't show anything only a blank. The option test is visible. I am connected to my database but this is on the top of the page were my other code is.

I would like to have it like this:

DROPDOWN MENU -vs- DROPDOWN MENU

I have searched on google for this but can't find it.

Hope you can help me.

This is my whole code

<?php
    if(!isset($_COOKIE['E2ingelogd'])) {
        header("location:../../index.php");
    }

    $username = "root";
    $password = "";
    $hostname = "localhost";

    $dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");

    $selected = mysql_select_db("login", $dbhandle);



        if(isset($_POST['team'])){
            $team = $_POST['team'];
            $ID = $_POST['id'];         

            $query = mysql_query("SELECT * FROM e2teams WHERE Team='$team' and ID='$ID'");
            if(mysql_num_rows($query) > 0 ) { //check if there is already an entry for that username
                echo "$team bestaat al!";
            }
            else{
                mysql_query("INSERT INTO e2teams (Team) VALUES ('$team')");
                header("location:e2admin.php");
            }
    }

    mysql_close();
?>

<html><head>
<link href='http://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>
<link href="../css/layout.css" rel="stylesheet" type="text/css"></head>
    <body>
    <div class="wrapper">
            <div class="header">
                <div class="logo"><img height="140" src="../images/boyslogo.png"> </div>
                <div class="titelpagina">Vroomshoopse Boys E2 admin panel</div>
            </div>
      <div class="content">
                <div class="teamstoevoegenvak">
                    <div class="titelbalk">
                        <h1>Voeg teams toe</h1>

                            <form style="border:0px; margin:0px; padding:0px"; action="e2admin.php" method="POST">
                                <input width="400" maxlength="400" type="text" name="team" placeholder="Team naam" /><br>
                                <input type="submit" value="Toevoegen" />
                            </form></div>
                 </div>
                    <div clas="toegevoegdeteamsvak">
                    <div class="titelbalktoege">
                        <h1>Toegevoegde teams</h1>
                    </div>
                   <div class="deteams">
                    <?php
                        $table = "e2teams";
                        $sql = "SELECT * FROM e2teams";
                        $result = mysql_query($sql, $dbhandle);
                        if(mysql_num_rows($result) > 0){
                            while($row = mysql_fetch_array($result)) {
                                echo "<table><tr><td class='styled-td'>";
                                echo $row['Team']. '</td><td></td><td><a href="edit.php?edit='.$row['ID'].'">Bewerk</a><br>';
                                echo "</td></tr></table>";
                            }
                        }
                    ?>
                    </div>
                    </div>
      </div>
        </div>
        <form action="" method="post">
        <select name="dropdown">
            <option value=""><?php echo $row['Team'] ?></option>
            <option value="">test</option>
            </select>
            </form>
    </body>
</html>


Solution :

Right in your own code you do this:

if(mysql_num_rows($result) > 0){
    while($row = mysql_fetch_array($result)) {
        echo "<table><tr><td class='styled-td'>";
        echo $row['Team']. '</td><td></td><td><a href="edit.php?edit='.$row['ID'].'">Bewerk</a><br>';
        echo "</td></tr></table>";
    }
}

But then down where you're having trouble, no check, no while loop.

You might need to reset the pointer like:

mysql_data_seek($result, 0);

Then loop over the option line like:

<form action="" method="post">
<select name="dropdown">
<?php
if(mysql_num_rows($result) > 0){
    while($row = mysql_fetch_array($result)) {
       echo '<option value="">' . $row['Team'] . '</option>';
    }
}
?>
    <option value="">test</option>
</select>
</form>

You also probably want to put something like teamID into the value of the option.


Mysql Tutorials

Mysql Howto..

With MySQL how to distinguish between a NULL value and a non-existent value

How to use SSH input mysql database?

How to show top 3 fields from a column into horizontal row in mysql?

How long is too long data for any column in MySql

How to select all fields where a string is common in all the tables in a mysql database? [closed]

How do I do a mysql join and limit the number of identical results for the leftmost table to 1?

how to save multiple rows in mysql using codeigniter

How to organize data from the database?

how to submit the option selected from drop down list in jsp page to perform a mysql query

How to generate time and date in C# and format it for mysql?

How to get the largest value from a table in MySQL?

How to update a mysql column with php?

How to fetch dates between the from date and to date from a field which is stored as varchar in mysql

How can Upgrade MySQL 5.5.40 to MySQL 5.7

how to store and retrieve array in mysql [closed]

How to fetch data from MySQL database into Excel

PHP/MySQL: How to get multiple values from a PHP database method

How long will a temporary MEMORY table persist if I don't drop it (MySQL) [closed]

How to format date in Query Statement Mysql?

How to convert my database ID number to text before CSV export?

How to stop mysqld

How can I let users register my product online?

MySQL: how to extract multiple text files from my MySQL table

How do I create a unique combination constraint/index in MySQL? (NOT permutation)

how to set bulk_insert_buffer_size in mysql?

vb.net with mysql how to pick between to dates of different years?

How can I change a MySQL column from VARCHAR to date time and convert the data at the same time?

How to insert two different check box values into different columns of one mysql table using php?

How to easy upload content to MySql database in Java using JDBC? [closed]

how to fetch a mysql table and order it by date