Hello again from MrMaverick,
I am having trouble diagnosing and fixing this script that I modified from another person’s comment C.M.S. tutorial.
The purpose (hopefully) is for this script to connect to a database, retrieve all of the comment info stored, store them into an associative array (I guess…), then, using a while loop, print out each comment until there are no more rows to print. At least, that is the idea I got from looking at it. But unfortunately it’s not working out so well.
Here is probably some helpful information for you experts out there.
PHP Error Information:
Notice: Undefined index: title in ~\cmtCMS.php on line 33
Notice: Undefined index: cmt_sum in ~\cmtCMS.php on line 35
Notice: Undefined variable: cmt_entry_display in ~\cmtCMS.php on line 53
CODE: cmtCMS.php
[php]
<?php
class cmtCMS {
var $host;
var $user;
var $pass;
var $database;
//This Function will run the code to display all comments on the website submitted by users.
public function display_public() {
//Formulating the MySQL Loading Query
$query = "SELECT * FROM `comment_table` ORDER BY `cmt_date` DESC LIMIT 6";
//Query the Database
$result = mysql_query($query);
// This shows the query sent to MySQL, and the error. Comment out when done debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
//If we have rows to fetch...
if ( $result == true && mysql_num_rows($result) > 0 ) {
// If there ARE rows to fetch, as long as we have results from the queries, keep inserting them into the array
while ( $array = mysql_fetch_assoc($result) ) {
/* Insert Database Information into variables from the Associative Array
We are assuming that the data being pulled from the database is clean of any malicious code */
$cmt_title = $array['title'];
$cmt_raw_date = $array['cmt_date'];
$cmt_subject = $array['cmt_sum'];
$cmt_data = $array['cmt_data'];
$username = $array['username'];
$rating = $array['cmt_rating'];
//Format the MySQL Date into the correct format we desire for each post
$cmt_date_proc = date('F jS, Y \a\t g:i A', strtotime( $cmt_raw_date ));
//Display the Post Data
$cmt_entry_display .= <<<CMT_ENTRY_DISPLAY
$cmt_title
Posted By: $username on $cmt_date_proc [$cmt_subject]
$cmt_data
| Average Ratings Go Here: ($rating) |
CMT_ENTRY_DISPLAY;
}
}
else{
$cmt_entry_display .= <<<CMT_ENTRY_DISPLAY
No Comments To Load
Comment Info Appears Here
This is a hardcoded comment. Either someone didn't post anything, or someone managed to hack the database server to DROP ALL THINGS! Ah well, programmers live and learn (:P).
| Average Ratings Would Go Here: (#.##) |
CMT_ENTRY_DISPLAY;
}
//END display_public
}
//This Function will instruct PHP to connect to the database where the comments are stored.
public function connect() {
mysql_connect($this->host,$this->user,$this->pass) or die("Connection Error! Reason: " . mysql_error());
mysql_select_db($this->database) or die("Database Error! Reason: " . mysql_error());
}
}
?>
[/php]
While this is where the errors are being reported by PHP, this may not be the source. So I have included the PHP code on the page that accesses this script.
CODE: dd-cmt.php [Partial]
[php]
<?php
echo "Comments are unavailable to view because I am an idiot. That is all.";
//Include The Comment Content Management System ONLY ONCE
include_once('content/cmtCMS.php');
//Initialize the new class of variables to use in the Comment CMS
$cmt_obj = new cmtCMS();
//These are used as arguments for the connection function inside
$cmt_obj->host = 'localhost';
$cmt_obj->user = 'username';
$cmt_obj->pass = 'password';
$cmt_obj->database = 'database';
$cmt_obj->connect();
//Now just say to users that this is where actual comments will start
$cmt_obj->display_public();
?>
<?php include "footer/footer.php" ?>
[/php]