Jump to content
DeckDekk

Inserting data from dropdown into MySQL table

Recommended Posts

Hello there!

I've been banging my head on  this for a while and I just  can't seem to get it to work properly.

I have a dropdown menu which selects information from table1 using a select statement (this table is called 'lid'). It selects the firstname, lastname and member id from this table and shows it in the dropdown menu. I'm glad I got that part working but the hard thing is inserting the data that the user selects into another table.  So when you select the id member from this dropdown menu it only inserts a blank row into table2 (which is called 'teamlid'). Can you guys help me? How can I insert the id member into my table2? What am I doing wrong here?

Thanks a million! This is my first post so if I'm doing anything wrong, let me know and I'll fix it asap!

My code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Boast & Drive</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        .wrapper{
            width: 650px;
            margin: 0 auto;
        }
        .page-header h2{
            margin-top: 0;
        }
        table tr td:last-child a{
            margin-right: 15px;
        }
    </style>
</head>
<body>
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="page-header clearfix">
                        <h2 class="pull-left">Teamleden</h2>
                        <div class="btn-toolbar">
                        <a href="read.php" class="btn btn-primary btn-lg pull-right">Terug</a>
                        </div>
                    </div>
                    
                    <?php
                    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
                    error_reporting(E_ALL);

                    //Verbinding maken met de database
                    require_once "login.php";

                    $sql = "SELECT tl.teamnaam,
                                   tl.tl_ID,
                                   tl.lidnummer,
                                   l.voornaam,
                                   l.achternaam
                            
                            FROM   teamlid tl
                            
                            JOIN    lid l   ON tl.lidnummer = l.lidnummer
                            
                            ORDER BY tl.teamnaam;";  
                    
                    if($result = mysqli_query($conn, $sql)) {
                        if(mysqli_num_rows($result) > 0) {
                            echo "<table class='table table-bordered table-striped'>";
                                echo "<thead>";
                                    echo "<tr>";
                                        echo "<th>Teamnaam</th>";
                                        echo "<th>Tl_ID</th>";
                                        echo "<th>Lidnummer</th>";
                                        echo "<th>Voornaam</th>";
                                        echo "<th>Achternaam</th>";
                                    echo "</tr>";
                                echo "</thead>";
                                echo "<tbody>";
                                while($row = mysqli_fetch_array($result)){
                                    echo "<tr>";
                                        echo "<td>" . $row['teamnaam'] . "</td>";
                                        echo "<td>" . $row['tl_ID'] . "</td>";
                                        echo "<td>" . $row['lidnummer'] . "</td>";
                                        echo "<td>" . $row['voornaam'] . "</td>";
                                        echo "<td>" . $row['achternaam'] . "</td>";
                                        echo "<td>";
                                            echo "<a href='update.php?id=". $row['lidnummer'] ."' title='Gegevens wijzigen' data-															toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
                                            echo "<a href='delete.php?id=". $row['lidnummer'] ."' title='Lid verwijderen' data-																toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
                                        echo "</td>";                                        
                                    echo "</tr>";
                                }
                                echo "</tbody>";
                            echo "</table>";

                            mysqli_free_result($result);
                        } else{
                            echo "<p class='lead'><em>Er zijn geen gegevens om weer te geven.</em></p>";
                        }
                    } else{
                        echo "De volgende fout is gevonden:  " . mysqli_error($conn);
                    }
                    
                    ?>

                    
                    <form name="dropdown" method="post">
                     <div class="page-header clearfix">
                        <h2 class="pull-left">Teamlid toevoegen</h2>
                    </div>           
                    <p>Selecteer hieronder met behulp van het dropdown menu een lid welke je aan bovenstaand team wilt toevoegen</p>
                        
                    <div class="container-fluid">
                        <div class="row">

                        <?php
                        // Variabelen aanmaken en tonen met lege waardes
                        $teamnaam = $lidnummer = '';

                        // Code voor dropdown. Selecteert voornaam, achternaam en lidnummer van tabel lid)
                        $sql = "SELECT voornaam, achternaam, lidnummer FROM lid ORDER BY achternaam";
                        $result = mysqli_query($conn, $sql);

                        echo "<select id='teamLid' name='teamLid'>";
                            echo "<option>--Selecteer Lid--</option>";
                        while ($row = mysqli_fetch_array($result)) {
                            echo "<option value='" . $row['lid'] . "'>" . $row['voornaam'] . "  " . $row['achternaam'] . " " . $row['lidnummer'] . "</option>";
                        }
                        echo "</select>";
                        
                        if 
                        (isset($_POST["id"]) && !empty($_POST["id"])) {
                            $id = $_POST["teamLid"];

                            $stmt = $conn->prepare("INSERT INTO teamlid (teamnaam, lidnummer) VALUES (?,?)");
                            $stmt->bind_param('si', $param_teamnaam, $param_lidnummer);
                            $param_teamnaam         = $teamnaam;
                            $param_lidnummer        = $lidnummer;
                            $stmt->execute();

                        }
                        // Verbinding sluiten
                        mysqli_close($conn);
                        ?>
                            <div>
                                <input type="hidden" name="id" value="<?php echo $id; ?>" />
                                <input type="submit" name="submit" class="btn btn-primary" value="Toevoegen">
                            </div>
                        </div>
                    </div>
                </form>
            </div>  
        </div>
</body>
</html>

 

Share this post


Link to post
Share on other sites
$teamnaam = $lidnummer = '';

That's the only time you ever assign values to those variables.

  • Like 1

Share this post


Link to post
Share on other sites

You're right about that. But to get the input from the dropdown, what value should I assign to those variables?
(the reason I assigned empty strings to those variables is because I copied them from my other application page where I used an input form to get the data. I thought if I use that as a basis and work on from there it would help, but alas).

Share this post


Link to post
Share on other sites
4 minutes ago, DeckDekk said:

You're right about that. But to get the input from the dropdown, what value should I assign to those variables?

Here is a good place to start.

  • Great Answer 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.