博客,添加帖子不起作用

时间:2017-05-15 08:02:03

标签: php html post pdo blogs

所以我创建了一个带有添加帖子功能的博客。

这是我添加帖子的代码

  <?php
include_once('resources/init.php');

if(isset($_POST['title'],$_POST['contents'])){

    $errors = array();

    $title      = trim($_POST['title']);
    $contents   = trim($_POST['contents']);

    if(empty($title)){
     $errors[] = 'You need to supply a title';
    }
    else if(strlen($title)>255){
     $errors[] = 'The title can not be longer than 255 characters';   
    }

    if(empty($contents)){
     $errors[] = 'You need to supply some text';   
    }


    if(empty($errors)){
        add_post($title,$contents,$_POST);



        header("Location:index.php?id={$id}");
        die();
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Add Post</title>
        <style>
            label{display: block;}
            ul{list-style: none;}
            li{display: inline; margin-right: 20px;}
        </style>
    </head>
    <body>
    <nav>
        <ul>
            <li><a href='index.php' >Index</a></li>
            <li><a href='add_post.php' >Add a Post</a></li>

            <!--li><a href='' ></a></li-->
        </ul>
     </nav>
     <h1>Rogier's blog</h1>
        <h2>Add a Post</h2>
        <?php
        if(isset($errors) && !empty($errors)){
            echo"<ul><li>",implode("</li><li>",$errors),"</li></ul>";
        }
        ?>
        <form action='' method='post'>
     <div>
        <label for='title'>Title</label>
         <input type='text' name='title' value='<?php if(isset($_POST['title'])) echo $_POST['title']; ?>' />
     </div>
     <div>
         <label for='contents'>Content</label>
         <textarea name='contents' cols=20 rows=10><?php if(isset($_POST['contents'])) echo $_POST['contents']; ?></textarea>
      </div>

     <p><input type='submit' value='Add Post' /></p>
     </form>
    </body>
</html>

这是我的index.php

 <?php
include_once('verwerk.php');
include_once('resources/init.php');
//$posts = (isset($_GET['id'])) ? get_posts($_GET['id']) : get_posts();
$posts = get_posts((isset($_GET['id']))? $_GET['id'] : null); 
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Blog</title>
        <style>
            ul{list-style: none;}
            li{display: inline; margin-right: 20px;}
        </style>
    </head>
    <body>
     <nav>
        <ul>
            <li><a href='index.php' >Index</a></li>
            <li><a href='add_post.php' >Add a Post</a></li>
            <!--li><a href='' ></a></li-->
        </ul>
     </nav>
     <h1>Rogier's blog</h1>

     <?php
     foreach($posts as $post){
      ?>
     <h2><a href='index.php?id=<?php echo $post['post_id']; ?>' ><?php echo $post['title']; ?></a></h2>
     <p>
        Posted on <?php echo date('d-m-y h:i:s',strtotime($post['date_posted'])); ?>
        In  AMP<?php echo $post['name']; ?></a>
     </p>
     <div><?php echo nl2br($post['contents']); ?></div>
     <menu>
        <ul>
            <li><a href='delete_post.php?id=<?php echo $post['post_id']; ?>' >Delete This Post</a></li>
            <li><a href='edit_post.php?id=<?php echo $post['post_id']; ?>' >Edit This Post</a></li>
        </ul>
     </menu>
     <?php   
     }
     ?>
    </body>
</html>

所以问题是,当我点击添加帖子时,它会把我带到add_post.php,在那里我可以写下来并添加它,唯一的问题是当我点击添加帖子时它会把我带回index.php但是我的帖子没有显示。 有人可以帮我解决这个问题吗?

感谢。

0 个答案:

没有答案