PHP,Prefill表格,包含来自数据库,Bootstrap Modal的数据

时间:2015-06-15 19:38:38

标签: php html mysql twitter-bootstrap mysqli

我想预先填写一个应该编辑数据库中条目的表单。当我单击一行中的“编辑”时,模式应显示为预填充表单。到目前为止,一切工作正常,但我不知道如何更改我的代码以获得预填充表单的Modal。

<?php $page='database'; $php='yes'; $title='Datenbank' ; include( 'header.php'); ?>

<body>
    <?php include( 'menu.php'); ?>

    <div class="container">
        <h1>Datenbank</h1>


        <button class="btn btn-default" data-toggle="modal" data-target="#addToDatabase">Neuer Eintrag</button>

        <br><br>
        <p style="font-size: 16px; "><strong>Datenbankeinträge</strong></p>

        <?php
            $servername = "localhost";
            $username = "xxx";
            $password = "*******";
            $dbname = "xxx";

            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
                 die("Connection failed: " . $conn->connect_error);
            } 

            $sql = "SELECT id, name, year, regie, actors, length FROM films";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                echo "<div style=\"overflow: auto\"><table> <tr> <th>ID</th> <th>Filmname</th> <th>Jahr</th> <th>Regie</th> <th>Schauspieler</th> <th>Länge [Minuten]</th> <th><a data-toggle=\"modal\" data-target=\"#updateDatabase \"><i class=\"glyphicon glyphicon-pencil\"></i></a></th> </tr>";
                 // output data of each row
                while($row = $result->fetch_assoc()) {
                     $id = $row["id"];
                     echo "<tr><td>". $row["id"]. "</td><td>". $row["name"]. "</td><td>". $row["year"] . "</td><td>". $row["regie"] . "</td><td>". $row["actors"] . "</td><td>". $row["length"] . "</td><td><a data-toggle=\"modal\" data-target=\"#updateDatabase \"><i class=\"glyphicon glyphicon-pencil\"></i></td></tr>";
                }
            } else {
                 echo "Noch keine Einträge vorhanden. Bitte welche hinzufügen...";
            }

            echo "</table></div>";

            $conn->close();
        ?>


        <div id="addToDatabase" class="modal fade" role="dialog">
            <div class="modal-dialog modal-lg">

                <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">×</button>
                        <h4 class="modal-title">Neuen Film zur Datenbank hinzufügen</h4>
                    </div>
                    <div class="modal-body">
                        <form method="post" action="insertAtDatabase.php">
                            <div class="input-group margin-form">
                                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-film"></i></span>
                                <input type="text" name="name" class="form-control" placeholder="Filmname" value="1" aria-describedby="basic-addon1">
                            </div>
                            <div class="input-group margin-form">
                                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-calendar"></i></span>
                                <input type="number" name="year" class="form-control" placeholder="Erscheinungsjahr" aria-describedby="basic-addon1">
                            </div>
                            <div class="input-group margin-form">
                                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-star"></i></span>
                                <input type="text" name="regie" class="form-control" placeholder="Namen der Regie" aria-describedby="basic-addon1">
                            </div>
                            <div class="input-group margin-form">
                                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span>
                                <input type="text" name="actors" class="form-control" placeholder="Namen der Schauspieler" aria-describedby="basic-addon1">
                            </div>
                            <div class="input-group margin-form">
                                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-time"></i></span>
                                <input type="number" name="length" class="form-control" placeholder="Länge in Minuten" aria-describedby="basic-addon1">
                            </div>

                            <button type="submit" name="submit" class="btn btn-default margin-form">Übermitteln</button>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Schließen</button>
                    </div>
                </div>

            </div>
        </div>


        <div id="updateDatabase" class="modal fade" role="dialog">
            <div class="modal-dialog modal-lg">

                <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">×</button>
                        <h4 class="modal-title">Eintrag ändern</h4>
                    </div>
                    <div class="modal-body">
                        <form method="post" action="updateAtDatabase.php">
                            <div class="input-group margin-form">
                                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-wrench"></i></span>
                                <input type="number" name="id" class="form-control" placeholder="ID" aria-describedby="basic-addon1">
                            </div>
                            <div class="input-group margin-form">
                                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-film"></i></span>
                                <input type="text" name="name" class="form-control" placeholder="Filmname" aria-describedby="basic-addon1">
                            </div>
                            <div class="input-group margin-form">
                                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-calendar"></i></span>
                                <input type="number" name="year" class="form-control" placeholder="Erscheinungsjahr" aria-describedby="basic-addon1">
                            </div>
                            <div class="input-group margin-form">
                                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-star"></i></span>
                                <input type="text" name="regie" class="form-control" placeholder="Namen der Regie" aria-describedby="basic-addon1">
                            </div>
                            <div class="input-group margin-form">
                                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span>
                                <input type="text" name="actors" class="form-control" placeholder="Namen der Schauspieler" aria-describedby="basic-addon1">
                            </div>
                            <div class="input-group margin-form">
                                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-time"></i></span>
                                <input type="number" name="length" class="form-control" placeholder="Länge in Minuten" aria-describedby="basic-addon1">
                            </div>

                            <button type="submit" name="submit" class="btn btn-default margin-form">Übermitteln</button>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Schließen</button>
                    </div>
                </div>

            </div>
        </div>
    </div>

    <?php include( 'footer.php'); ?>
</body>

您还需要了解其他内容: 我正在使用最新版本的Bootstrap以获得Modal

0 个答案:

没有答案