php - 如何将内容动态创建到数据库中并显示在网页上?

标签 php mysql

我有四个 php 页面:login.php、db_connection.php、admin.php 和 blog.php

我希望能够通过 admin.php 页面将文章动态插入到 blog.php 中。

我的代码是:

登录.php

<?php 
    error_reporting(E_ALL & ~E_NOTICE); 
    session_start();

    if($_POST['submit']) {
        $dbUserName = "admin";
        $dbPassword = "password";

        $username = strip_tags($_POST['username']);
        $username = strtolower($username);
        $password = strip_tags($_POST['password']);

        if ($username == $dbUserName && password == $dbPassword) {
            $_SESSION['username'] = $username;
            header('Location: admin.php');
        } else {
            echo "<h1 class='denied'>Access denied<h1>";
        }
    }
?>

<div id="login_box">
    <h1 class="loreg">Log in</h1>
    <form action='login.php' method='post' id='contact_form'>
        <input type='text' name='username' placeholder='username... *' id='email' maxlength='60'><br>
        <input type='password' name='password' placeholder='password... *' id='password' maxlength='30'><br>
        <input type='submit' class='button' value='log in' id='login'>
        <input type='reset' class='button' value='cancel' id='cancel'>
    </form>
</div>

数据库连接

<?php  
    $dbCon = mysqli_connect("localhost", "root", "passw0rd", "worldtour") or die(mysqli_error());
?>

管理员.php

<?php 
    error_reporting(E_ALL & ~E_NOTICE); 
    session_start();

    if (isset($_SESSION['username'])) {
        $username = ucfirst($_SESSION['username']);

        if ($_POST['submit']) {
            $title = $_POST['title'];
            $submit = $_POST['subtitle'];
            $content = $_POST['content'];
            include_once("db_connection.php");
            $sql = "INSERT INTO blog (title, subtitle, content)
                    VALUE ('$title', '$subtitle', '$content')";
            mysqli_query($dbCon, $sql);
            echo "<h1>Blog entry posted</h1>";
        } 
    } else {
        header('Location: login.php');
        die();
    }
?>

<h1>Welcome, <?php echo $username; ?>!</h1>
<form action="admin.php" method="post">
    <h3>Title: </h3><input type="text" name="title"><br>
    <h3>Subtitle: </h3><input type="text" name="subtitle"><br>
    <h3>Content: </h3><textarea name="content" id="content" cols="30" rows="10"></textarea>
    <input type="submit" name="submit" value="post entry">
</form>
<br>
<a href="/sites/worldtour/public/blog.php">View blog entries</a> | <a href="/sites/worldtour/public/logout.php">Log out</a>

博客.php

<?php 
    include_once("db_connection.php");

    $sql = "SELECT * FROM blog ORDER BY id DESC";
    $result = mysqli_query($dbCon, $sql);

    while($row = mysqli_fetch_array($result)) {
    $title = $row['title'];
    $subtitle = $row['subtitle'];
    $content = $row['content'];
?>
    <h1 class="headers"><?php echo $title; ?> <br> <small><?php echo $subtitle; ?></small></h1>
    <article><?php echo $content; ?></article>
    <hr class="artline">
<?php  
    }
?>

当我尝试通过 login.php 页面登录时,即使我输入了正确的用户/密码,页面也会刷新并且不会将我重定向到 admin.php。当我插入错误的用户时,它不会提示我拒绝访问消息。我的代码中是否有任何错误或我错过了什么?谢谢。

最佳答案

POST 方法使用标签中的名称属性来标识其值。在 $_POST['submit'] 中,您实际上是在寻找 name='submit' 的标签。

提交按钮没有定义名称属性。您需要在

中添加 name='submit'

<input type='submit' class='button' value='log in' id='login'>

关于php - 如何将内容动态创建到数据库中并显示在网页上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31638800/

相关文章:

php - Sql sum() 来自不同表的列

php - 在 Ajax : any configuration difference between Nginx and Apache

php - Wordpress 限制 - 系统设计考虑

PHP 如何一次多次插入表

php - 当没有列关系时从第二个表中检索数据

php - 使用内部连接为三个表编写 mysql 查询

mysql - 我可以将 Grails 4 应用程序部署到 Google Cloud Platform 吗?

php - URL 中的魔术文件夹名称

php - 查询显示数据库中的四个随机数据

mysql - 对于这个(不允许的)带有嵌套 SELECT 的 UPDATE,是否有替代方法?