PHP - Help starting this project

General discussions related to php

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Sat Jun 22, 2013 9:34 am

PHP - Help starting this project

Postby blert » Sat Jun 22, 2013 9:35 am

I'm relatively new at PHP and have learned the basics in string, arrays, and objects... but I don't feel I'm strong enough in my knowledge base for creating a simple Commments section for my website/blog. Since PHP is behind the scenes, it's rather hard to figure out how to do this without "paying" for the code or using a pre-made (ad-based) service such as DISQUS because the code just doesn't show in the browser (obviously). I was wondering if someone could give me a basic idea (or even a little starter code) to get me started, I just need to see the backend behind it to get an idea on what to do next.

The comments section I want only requires the person's name and the comment itself taken in the form below...

Code: Select all

    <form action="/php/post-comment.php" method="post" id="commentform">
       <label for="author"><small>Name (required)</small></label></br>
       <input type="text" name="author" id="author" value="" size="22" tabindex="1" aria-required="true" />
       <label for="comment"><small>Comment: (required)</small></label></br>
       <textarea name="comment" id="comment" width="80" rows="10" tabindex="4"></textarea>
       <input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />

php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: PHP - Help starting this project

Postby johnj » Thu Jun 27, 2013 8:36 pm

You have a form that collects name and comments. Now, write the sql which will pick these values and insert into the database/table.

User avatar
php-forum Active User
php-forum Active User
Posts: 253
Joined: Sat Mar 23, 2013 8:24 am

Re: PHP - Help starting this project

Postby Strider64 » Fri Jun 28, 2013 5:38 am

filename: simpleCMS.php

Code: Select all

 *  A Very Simple CMS Tutorial
 *  by John R. Pepp
 *  Date: June 28, 2013

// This is a very simple CMS tutorial and I just want to point of a few
// things before I continue any further:
// 1. Don't Use This for a Live Website AS IS!!!
// 2. See Rule One!

//    `id` int(11) NOT NULL AUTO_INCREMENT,
//    `creatorId` int(11) NOT NULL,
//    `title` varchar(40) NOT NULL,
//    `comment` text NOT NULL,
//    `dateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
//    PRIMARY KEY (`id`)

// I'm going to use the PDO way of connecting to a database in this tutorial.

/*   This normally goes into some kind of utility file */
$db_options = array(
         // important! use actual prepared statements (default: emulate prepared statements)
            PDO::ATTR_EMULATE_PREPARES => false 
         // throw exceptions on errors (default: stay silent)                   
         // fetch associative arrays (default: mixed arrays)           
// This sets up the database connection:      
$pdo = new PDO('mysql:host=localhost;dbname=myfirstcms;charset=utf8', 'username', 'password', $db_options);
/*   For example - require 'includes/'; */   

// Check to see if user has enter a comment:
if (isset($_POST['submit']) && $_POST['submit'] == 'submit') {

  $creatorId = 45; // This is where you would get the Id of the user
               // but that is for a different tutorial;
  // Grab the data from the form:
  $title = htmlspecialchars($_POST['title']);
  $comment = htmlspecialchars($_POST['comment']);
  // Insert into the database using PDO and prepared statements:
  $query = 'INSERT INTO pages (creatorId, title, comment, dateAdded) VALUES (:creatorId, :title, :comment, NOW())';
  $stmt = $pdo->prepare($query);
  $result = $stmt->execute(array(':creatorId' => $creatorId, ':title' => $title, ':comment' => $comment));
  // Well that is all there is to enter a comment into a table,
  // I used a ternay operator below to show if this worked.

<meta charset="utf-8">
<title>Simple CMS Tutorial</title>
body {
   font-size: 76%;
   background-color: #f9f9f9;
   padding: 0px;
   margin: 0px;
section {
   padding: 20px;
input#title {
   width: 616px;
   height: 25px;
   border: 1px solid #010101;
   background-color: #ccc;
textarea#comment {
   outline: none;
   overflow: hidden;
   width: 600px;
   height: 400px;
   border: 1px solid #010101;
   background-color: #ccc;
   font-family: Tahoma, sans-serif;
   font-size: 1.2em;
   line-height: 1.5em;
   color: #010101;
   resize: none;
   padding: 8px;
   margin: 0px 0px 10px 0px;
#submit {
   margin-left: 525px;

  <h3><?php echo (isset($result) && $result) ? 'Congrats, comment entered into Database' : 'Simple CMS'; ?></h3>
  <form action="simpleCMS.php" method="post">
    <label for="title">Title:</label>
    <input type="text" name="title" id="title" />
    <label for="comment">Comment: </label>
    <textarea name="comment" id="comment"></textarea>
    <input type="submit" name="submit" value="submit" id="submit" />

Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 1 guest