PHP - Help starting this project

General discussions related to php

Moderators: macek, egami, gesf

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

Re: PHP - Help starting this project

Post by 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
Strider64
php-forum Active User
php-forum Active User
Posts: 286
Joined: Sat Mar 23, 2013 8:24 am

Re: PHP - Help starting this project

Post by Strider64 » Fri Jun 28, 2013 5:38 am

filename: simpleCMS.php

Code: Select all

<?php
/****
 *  
 *  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!

//  CREATE TABLE IF NOT EXISTS `pages` (
//    `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`)
//  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

// 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)                   
            , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
			// fetch associative arrays (default: mixed arrays)           
            , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC      
        );
		
// This sets up the database connection:		
$pdo = new PDO('mysql:host=localhost;dbname=myfirstcms;charset=utf8', 'username', 'password', $db_options);
/*   For example - require 'includes/utilities.inc.php'; */    

// 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.
	
}

?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Simple CMS Tutorial</title>
<style>
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;
}
</style>
</head>

<body>
<section>
  <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>
    <br>
    <input type="text" name="title" id="title" />
    <br>
    <br>
    <label for="comment">Comment: </label>
    </br>
    <textarea name="comment" id="comment"></textarea>
    <br>
    <input type="submit" name="submit" value="submit" id="submit" />
  </form>
</section>
</body>
</html>

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest