Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在数据库中动态创建内容并将其显示在网页上?_Php_Mysql - Fatal编程技术网

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

Php 如何在数据库中动态创建内容并将其显示在网页上?,php,mysql,Php,Mysql,我有四个php页面:login.php、db_connection.php、admin.php和blog.php 我希望能够通过admin.php页面将文章动态插入blog.php 我的代码是: login.php <?php error_reporting(E_ALL & ~E_NOTICE); session_start(); if($_POST['submit']) { $dbUserName = "admin";

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

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

我的代码是:

login.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>

admin.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>

POST方法使用标记中的name属性来标识其值。在$\u POST['submit']中,您实际上正在查找名为='submit'的标记

“提交”按钮未定义“名称”属性。您需要在中添加name='submit'

<?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 
    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  
    }
?>