View Single Post
  #1 (permalink)  
Old March 17th, 2012, 09:13 AM
Master Yoda Master Yoda is offline
Authorized User
Points: 36, Level: 1
Points: 36, Level: 1 Points: 36, Level: 1 Points: 36, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default [SOLVED] Chapter 2 add_post.php issue

Hi there folks,

I seem to be having some issues in posting messages using this code. The add_forum.php worked without a problem, but everytime I try to post a message I get redirected to the view forum page. Nothing gets added to the database, although it does generate the error message if I leave the fields blank.

MySQL code for forum_message:

Code:
CREATE TABLE IF NOT EXISTS `ufw_forum_message` (
  `message_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `parent_message_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `forum_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `subject` varchar(100) COLLATE latin1_general_cs NOT NULL,
  `message_text` text COLLATE latin1_general_cs NOT NULL,
  `message_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`message_id`),
  KEY `parent_message_id` (`parent_message_id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs AUTO_INCREMENT=13 ;

MySQL code for forum:

Code:
CREATE TABLE IF NOT EXISTS `ufw_forum` (
  `forum_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `forum_name` varchar(50) COLLATE latin1_general_cs NOT NULL,
  `description` varchar(100) COLLATE latin1_general_cs NOT NULL,
  PRIMARY KEY (`forum_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs AUTO_INCREMENT=3 ;

I assume my problem is somewhere in the part of the code where its meant to update the database. I also wonder why message_date isn't included when the post is added (although its used in view)...

The code for add_post.php:
PHP Code:
<?php

// Include shared code.
// include '../lib/common.php'; -----> already included in 401.php
include '../lib/database.php';
include 
'../lib/functions.php';
include 
'../lib/class_user.php';
include 
'401.php';                                 // 401.php referenced since the user should be logged in to view this page.

// Retrieve user information.
$user User::getById($_SESSION['user_id']);
if (!
$user->user_id)
{
    die(
'<p>Sorry, you must be logged in to post.</p>');
}

// Validate incoming values.
$forum_id = (isset($_GET['fid'])) ? (int)$_GET['fid'] : 0;
$query sprintf('SELECT forum_id FROM %sforum WHERE forum_id = %d'DB_TBL_PREFIX$forum_id);
$result mysql_query($query$GLOBALS['DB']);
if (!
mysql_num_rows($result))
{
    
mysql_free_result($result);
    
mysql_close($GLOBALS['DB']);
    die(
'<p>Invalid forum id.</p>');
}
mysql_free_result($result);

$msg_id = (isset($_GET['mid'])) ? (int)$_GET['mid'] : 0;
$query sprintf('SELECT message_id FROM %sforum_message WHERE message_id = %d'DB_TBL_PREFIX$msg_id);
$result mysql_query($query$GLOBALS['DB']);
if (
$msg_id && !mysql_num_rows($result))
{
    
mysql_free_result($result);
    
mysql_close($GLOBALS['DB']);
    die(
'<p>Invalid message id.</p>');
}
mysql_free_result($result);

$msg_subject = (isset($_POST['msg_subject'])) ? trim($_POST['msg_subject']) : '';
$msg_text = (isset($_POST['msg_text'])) ? trim($_POST['msg_text']) : '';

// Add entry to the database if the form was submitted and the necessary values were supplied.
if (isset($_POST['submitted']) && $msg_subject && $msg_text)
{
    
$query sprintf('INSERT INTO %sforum_message (subject, message_text, parent_message_id, forum_id, user_id) VALUES ("%s", "%s", %d, %d, %d)'DB_TBL_PREFIXmysql_real_escape_string($msg_subject$GLOBALS['DB']), mysql_real_escape_string($msg_text$GLOBALS['DB']), $msg_id$forum_id$user->user_id);
    
mysql_query($query$GLOBALS['DB']);
    
// Redirect.
    
header('Location: view_forum.php?fid=' $forum_id . (($msg_id) ? '&mid=' $msg_id ''));
}

// Form was submitted but not all the information was correctly filled in.
else if (isset($_POST['submitted']))
{
    
$message '<p>Not all necessary information was provided. Please correct and resubmit.</p>';
}

// Generate the form.
ob_start();
if (isset(
$message))
{
    echo 
$message;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) . '?fid=' $forum_id '&mid=' $msg_id?>">
    <div>
        <label for="msg_subject">Subject:</label>
        <input type="input" id="msg_subject" name="msg_subject" value="<?php  echo htmlspecialchars($msg_subject); ?>"/><br />
        <label for="msg_text">Post Message:</label>
        <textarea id="msg_text" name="msg_text"><?php echo htmlspecialchars($msg_text); ?></textarea>
        <br />
        <input type="hidden" name="submitted" value="1"/>
        <input type="submit" value="Add Post"/>
    </div>
</form>
<?php 
$GLOBALS
['TEMPLATE']['content'] = ob_get_clean();

// Display the page.
include '../templates/basic_template.php';
?>
Any help would be greatly appreciated... and sorry about the long lines in the code
Is there any way to limit the size of the PHP box in this post?

Thanks!!

Last edited by Master Yoda; March 20th, 2012 at 06:32 AM. Reason: Solved - Solution was in the database error.
Reply With Quote