简体   繁体   中英

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

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>

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.

the problem is that you using that variable $row['Team'] outside the while loop , try this

  ?>

<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 class="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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM