Php 如何使用mysql查询发布新闻

Php 如何使用mysql查询发布新闻,php,html,mysql,laravel,Php,Html,Mysql,Laravel,目前,我正在尝试创建一个新闻系统,在该系统中,我使用ckeditor编写一篇文章,发布后,该字段将输入保存在mysql表中,并在索引页上显示已发布的新闻。我能够获取数据,以便它可以显示在索引页上,并且可以正常工作。我就是这样做的: <div class="panel-body"> <?php $db = getDB(); $stmt = $db->prepare("SELECT * FROM news ORD

目前,我正在尝试创建一个新闻系统,在该系统中,我使用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>


在新闻表中,将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>


CKEDITOR.replace('content'); 提交
您现在遇到的错误是什么?看:跳出来的是
$stmt->bindParam('title',
…应该是
'title:'
@amin masterking谢谢你这么多人,它就像一个符咒!
<?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>