2

Currently I'm trying to create a news system where I write an article using ckeditor and after I publish it, the field saves the inputs in a mysql table, displaying the published news at the index page. I'm able to fetch the data so that it can be displayed at the index page and it works. This is how I do it:

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

Now for being able to publish the news, I tried utilizing ckeditor and drafted this but it does not save the values in the fields at all.;

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

All I want is to be able to publish the news which would save in the table mentioned above in the code.

I'm sure I have made a lot of mistakes in the publishing code, which is why any help would be appreciated.

This is what it looks like when it displays the news at the index page. NOTE: The values seen in the picture were added manually in the table.

IMAGE FOR THE NEWS DISPLAY

Jose Vf
  • 1,493
  • 17
  • 26
  • What is the error you are getting now? See: [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) What jumps out is `$stmt->bindParam('title',`... shoud be `'title:'` – ficuscr Oct 16 '18 at 21:57

1 Answers1

2

In your news table change id field to primary key & auto-increment.

Try this :

<?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>
Amin.MasterkinG
  • 805
  • 1
  • 12
  • 23