将数据输入数据库后,如何在网页的表格中显示数据?

时间:2019-02-09 17:23:28

标签: php html

我不确定如何描述它,所以这里是video,我在这里解释我的问题。

我尝试重新排列一些代码,因为我确实认为没有问题,因此尝试确保表刷新了其中的新数据,但是每次我尝试以不同的顺序放置代码时(执行查询以不同的顺序进行),它的功能不同于我想要的功能,或者根本不起作用。

两个查询都可以单独起作用,我不确定为什么它们不能一起工作。

搜索栏具有在“搜索”页面和相关页面上的主页中输入的可见值。但是,此页面留空了,这使我得到了全表显示的结果,这正是我想要发生的事情。我只是不确定如何编辑代码,因此在提交时,它将显示新添加的数据。

我的PHP:

<?php
$find     = $_POST['searchbar'];
$host     = "localhost";
$username = "FFF";
$pword    = "L3FhqJNey8Op2qJY";
$database = "Project";

include 'includes/db.inc.php';

$Name2          = $_POST['Name'];
$YearOfRelease2 = $_POST['YearOfRelease'];
$Studio2        = $_POST['Studio'];
$Age2           = $_POST['Age'];
$Score2         = $_POST['Score'];
?> 

我的HTML:

<html>
    <head>
        <title>Add a Film - Films! Films! FILMS!</title>
    </head>
    <body>
        <h1>Films! Films! FILMS!</h1>
        <h2>Add a Film</h2>
        <p>If you wish to add a film to our database, feel free to add data relating to the film in the respective boxes below. You should then refresh the page.</p>
        <p>Add Film:</p>
        <form method="POST" action="AddFilm.php">
        <p>Name of Film: <input type="text" name="Name"></p>
        <p>Year of Release: <input type="text" name="YearOfRelease"></p>
        <p>Name of Studio: <input type="text" name="Studio"></p>
        <p>Age Rating: <select name="Age" size="1">
            <optgroup label="Select Age Rating">
                <option value="U">U</option>
                <option value="PG">PG</option>
                <option value="12">12</option>
                <option value="15">15</option>
                <option value="18">18</option>
            </optgroup>
        </select></p>
        <p>Review Score: <input type="text" name="Score"></p>

        <p><input type="submit" name="submit" value="Submit and Refresh"></p>
        </form>
        <?php
        echo "<h2>$output</h2>";
        $query_string = "SELECT * FROM movies WHERE Name LIKE '%$find%' OR YearOfRelease LIKE '%$find%' OR Studio LIKE '%$find%' OR Age LIKE '%$find%' OR Score  LIKE '%$find%'";

        $query_string2 = "INSERT INTO movies (Name, YearOfRelease, Studio, Age, Score) VALUES ('$Name2', '$YearOfRelease2', '$Studio2', '$Age2', '$Score2');";

        if ($result = $mysqli->query($query_string2)) {
            $output2 = $Name2 ." has been added to the database.";
            echo "<p>$output2</p>";

            } else {
            echo ("Error performing query: " . $mysqli->error() );
        }
        $result->close();

        if ($result = $mysqli->query($query_string)) {
            echo "<table border='1'>";
            echo "<tr><th>FilmID</th><th>Name</th><th>YearOfRelease</th><th>Studio</th><th>Age</th><th>Score</th></tr>";

        while ($row = $result->fetch_object())
            {
                $FilmID = $row->FilmID;
                $Name = $row->Name;
                $YearOfRelease = $row->YearOfRelease;
                $Studio = $row->Studio;
                $Age = $row->Age;
                $Score = $row->Score;

                $output ="<tr><td> $FilmID";
                $output = $output . "<td> $Name";
                $output = $output . "<td> $YearOfRelease";
                $output = $output . "<td> $Studio";
                $output = $output . "<td> $Age";
                $output = $output . "<td> $Score </tr>";

                echo "<p>$output</p>";
            }
            echo "</table>";
            echo "<hr>";
            echo '<p><a href="Home.html">Back to Home Page</a></p>';
            $result->close();
        } else {
            echo ("Error performing query: " . $mysqli->error() );
        }

        $mysqli->close();
        ?>
    </body>
</html>

0 个答案:

没有答案