如何使用mysql查询发布新闻

时间:2018-10-16 21:54:35

标签: php html mysql laravel

当前,我正在尝试创建一个新闻系统,在其中使用ckeditor撰写文章,并在发布该文章后,该字段将输入内容保存在mysql表中,从而在索引页面上显示已发布的新闻。我能够获取数据,以便可以将其显示在索引页面上并且可以工作。这是我的方法:

<div class="panel-body">
        <?php
            $db = getDB();
            $stmt = $db->prepare("SELECT * FROM news ORDER BY created_at DESC;");
            $stmt->execute();

            while($data = $stmt->fetch(PDO::FETCH_ASSOC)) {

                $title = $data['title'];
                $content = $data['content'];
                $date = $data['created_at'];
                $admin = (int)$data['admin'];
                $id = (int)$data['id'];

                $admin_data = new User($admin);
                $admin_name = $admin_data->getUsername();


                print("<div class='timeline-panel'>");

                    print("<div class='timeline-heading'>");
                        print ("<h4 class='timeline-title'><b>$title</b></h4>");
                        print ("<p><small class='text-muted'><i class='fa fa-clock-o'></i> $date $admin_name</small></p>");
                    print("</div>");

                    print("<div class='timeline-body'>");
                        print("<p>$content</p>");
                    print("</div>");

                print("</div>");
                print("</li>");
                print("<hr style='height:1px;border-top:2px solid #aa0000'>");
            }
        ?>
</div>

现在,由于能够发布新闻,我尝试使用ckeditor并起草了此消息,但它根本没有将值保存在字段中。

<script src="/ugrp/ckeditor/ckeditor.js"></script>
    <div id="wrapper">
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
            </div>
            <!-- /.col-lg-12 -->
        </div>

    <div class="col-md-12" style="padding: 20px 15px;">


            <?php

                if(isset($_POST['submit'])) {
                $db = getDB();
                $stmt = $db->prepare("INSERT INTO `news`(`id`, `admin`, `content`, `title`, `created_at`) VALUES (:title, :content, :created_at, :admin, :id)");

                $stmt->bindParam('title', $title, PDO::PARAM_STR);
                $stmt->bindParam('content', $content, PDO::PARAM_INT);
                $stmt->bindParam('created_at', $created_at, PDO::PARAM_INT);
                $stmt->bindParam('admin', $admin, PDO::PARAM_INT);
                $stmt->bindParam('id', $id, PDO::PARAM_INT);
                $stmt->execute();

                }

                ?>


    <form>

           <div class="form-group">
             <input name="title" type="text" id="title" class="form-control" placeholder="Title of the News"><br>
            </div>
                            <div class="form-group">
                            </div>
        <textarea name="content" id="content">

        </textarea>
        <script>
            CKEDITOR.replace( 'content' );
        </script>
        <center><button type="submit" name="submit" class="btn bg-red btn-block"><i class="fa fa-pencil"></i> Submit</button></center>
    </form>



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

我所希望的就是能够发布新闻,该新闻将保存在代码中上述表格中。

我确定我在发布代码中犯了很多错误,这就是任何帮助都将受到赞赏的原因。

这是在索引页上显示新闻时的样子。 注意:图片中显示的值是在表格中手动添加的。

IMAGE FOR THE NEWS DISPLAY

1 个答案:

答案 0 :(得分:2)

在新闻表中,将id字段更改为“主键和自动递增”。

尝试一下:

<?php
    if(isset($_POST['submit'])) {
        $db = getDB();
        $title = (!isset($_POST["title"])) ? 'Empty' : $_POST["title"];
        $content = (!isset($_POST["content"])) ? 'Empty' : $_POST["content"];
        $created_at = date("Y-m-d H:i:s");
        $admin = 1;//Value of user id --- $admin_data = new User($admin); $admin = $admin_data->getUserID();
        $stmt = $db->prepare("INSERT INTO `news`(`admin`, `content`, `title`, `created_at`) VALUES (:admin,:content,:title,:created_at)");

        $stmt->bindParam(':title', $title, PDO::PARAM_STR);
        $stmt->bindParam(':content', $content, PDO::PARAM_INT);
        $stmt->bindParam(':created_at', $created_at, PDO::PARAM_INT);
        $stmt->bindParam(':admin', $admin, PDO::PARAM_INT);
        $stmt->execute();
    }
?>
<form method="POST">
       <div class="form-group">
         <input name="title" type="text" id="title" class="form-control" placeholder="Title of the News"><br>
        </div>          
        <textarea name="content" id="content">
    </textarea>
    <script>
        CKEDITOR.replace( 'content' );
    </script>
    <center><button type="submit" name="submit" class="btn bg-red btn-block"><i class="fa fa-pencil"></i> Submit</button></center>
</form>