PHP - Help starting this project

General discussions related to php

Moderators: egami, macek, gesf

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

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: 315
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI

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

Post Reply