Following online tutorial-Need Help

Hello everyone,

I am very new to PHP and MySQL. I am currently following a tutorial and having couples of errors but I manage to solve most of them.
But there is one last thing to be completed. The tutorial is on a ‘Simple Forum Board’. Actually, I do not know how to describe this problem technically and precisely but I can explain by an example.

Whenever a ‘New Topic’ is created, the author name, time and date of the post will be displayed. Let say the author posting time is March 8, 2012, 10.20am. When another person reply to the topic at let say March 9, 2012, 4.50pm, the author posting time will change to March 9, 2012, 4.50pm as well. Meaning to say, the author posting time will change and same as the posting time of the last poster. I have put my best effort and did not find any solution yet.

Here’s the code
Index code


<?php

include "connect.php"; //mysql db connection here

print "<link rel='stylesheet' href='style.css' type='text/css'>";

print "<A href='post.php'>New Topic</a><br>";

print "<table class='maintable'>";

print "<tr class='headline'><td width=50%>Discussion Topic</td><td width=20%>Author</td><td>Replies</td><td>Last replied time</td></tr>";

$getthreads="SELECT * from forum where parentid='0' order by postid DESC";

$getthreads2=mysql_query($getthreads) or die("Could not get threads");


while($getthreads3=mysql_fetch_array($getthreads2))

{

  $getthreads3['title']=strip_tags($getthreads3['title']);

  $getthreads3['author']=strip_tags($getthreads3['author']);

  print "<tr class='mainrow'><td><a href='message.php?id=$getthreads3[postid]'>$getthreads3[title]</a></td><td>$getthreads3[author]</td><td>$getthreads3[numreplies]</td><td>
$getthreads3[showtime]<br>Last post by <b>$getthreads3[lastposter]</b></td></tr>";

}

print "</table>";



?>

<a href="forum.php?"><img src="image/button_back1.gif" border="0" align="right"/></a>

Post code


<?php

include "connect.php"; //connection string

print "<link rel='stylesheet' href='style.css' type='text/css'>";

print "<table class='maintables'>";

print "<tr class='headline'><td>Post a message</td></tr>";

print "<tr class='maintables'><td>";

if(isset($_POST['submit']))

{

   $name=$_POST['name'];

   $yourpost=$_POST['yourpost'];

   $subject=$_POST['subject'];

   if(strlen($name)<1)

   {

      print "You did not type in a name."; //no name entered

   }

   else if(strlen($yourpost)<1)

   {

      print "You did not type in a post."; //no post entered

   }

   else if(strlen($subject)<1)

   {

      print "You did not enter a subject."; //no subject entered

   }

   else

   {
	   date_default_timezone_set('Asia/Brunei');
	   $thedate=date("U"); //get unix timestamp
	
      $displaytime=date("F j, Y, h:i a");

      //we now strip HTML injections

      $subject=strip_tags($subject);

      $name=strip_tags($name);

      $yourpost=strip_tags($yourpost);

      $insertpost="INSERT INTO elec_forum(author,title,post,showtime,realtime,lastposter) values('$name','$subject','$yourpost','$displaytime',$thedate,'$name')";

      mysql_query($insertpost) or die("Could not insert post"); //insert post

      print "Message posted, go back to <A href='elec_forum.php'>Forum</a>.";

   }



}

else

{

   print "<form action='elec_post.php' method='post'>";

   print "Your name:<br>";

   print "<input type='text' name='name' size='20'><br>";

   print "Subject:<br>";

   print "<input type='text' name='subject' size='20'><br>";

   print "Your message:<br>";

   print "<textarea name='yourpost' rows='5' cols='40'></textarea><br>";

   print "<input type='submit' name='submit' value='submit'></form>";



}

print "</td></tr></table>";

?>

Reply code


<?php

include "connect.php"; //connection string

print "<link rel='stylesheet' href='style.css' type='text/css'>";

print "<table class='maintables'>";

print "<tr class='headline'><td>Reply</td></tr>";

print "<tr class='maintables'><td>";

if(isset($_POST['submit']))

{

   $name=$_POST['name'];

   $yourpost=$_POST['yourpost'];

   $subject=(isset($_POST['subject']))?$_POST['subject']:null;;


   $id=$_POST['id'];

   if(strlen($name)<1)

   {

      print "You did not type in a name."; //no name entered

   }

   else if(strlen($yourpost)<1)

   {

      print "You did not type in a post."; //no post entered

   }

   else

   {
      date_default_timezone_set('Asia/Brunei');
      $thedate=date("U"); //get unix timestamp

      $displaytime1=date("F j, Y, h:i a");

      //we now strip HTML injections

      $subject=strip_tags($subject);

      $name=strip_tags($name);

      $yourpost=strip_tags($yourpost);

      $insertpost="INSERT INTO elec_forum(author,title,post,showtime,realtime,lastposter, parentid) values('$name','$subject','$yourpost','$displaytime1','$thedate','$name','$id')";

      mysql_query($insertpost) or die("Could not insert post"); //insert post

      $updatepost="Update elec_forum set numreplies=numreplies+'1', lastposter='$name', showtime='$displaytime1',lastrepliedto='$thedate' where postid='$id'";

      mysql_query($updatepost) or die("Could not update post");
      print "Message updated, go back to <A href='elec_message.php?id=$id'>Message</a>.";

   }



}

else

{

   $id=$_GET['id'];

   print "<form action='elec_reply.php' method='post'>";

   print "<input type='hidden' name='id' value='$id'>";

   print "Your name:<br>";

   print "<input type='text' name='name' size='20'><br>";

   print "Your message:<br>";

   print "<textarea name='yourpost' rows='5' cols='40'></textarea><br>";

   print "<input type='submit' name='submit' value='submit'></form>";



}

print "</td></tr></table>";

?>

Message code


<?php

include "connect.php"; //mysql db connection here

$id=(isset($_GET['id']))?$_GET['id']:null;;


print "<link rel='stylesheet' href='style.css' type='text/css'>";

print "<A href='elec_forum.php'>Back to main forum</a>-<A href='post.php'>New Topic</a>-<A href='elec_reply.php?id=$id'>Reply<br>";

print "<table class='maintable'>";

print "<tr class='headline'><td width=20%>Author</td><td width=80%>Post</td></tr>";

$gettopic="SELECT * from forum where postid='$id'";

$gettopic2=mysql_query($gettopic) or die("Could not get topic");

$gettopic3=mysql_fetch_array($gettopic2);

print "<tr class='mainrow'><td valign='top'>$gettopic3[author]</td><td valign='top'>Last replied to $gettopic3[showtime]<br><hr>";

$message=strip_tags($gettopic3['post']);

$message=nl2br($message);

print "$message<hr><br>";

print "</td></tr>";

$getreplies="Select * from forum where parentid='$id' order by postid DESC"; //getting replies

$getreplies2=mysql_query($getreplies) or die("Could not get replies");

while($getreplies3=mysql_fetch_array($getreplies2))

{

   print "<tr class='mainrow'><td valign='top'>$getreplies3[author]</td><td valign='top'>Last replied to $getreplies3[showtime]<br><hr>";

   $message=strip_tags($getreplies3['post']);

   $message=nl2br($message);

   print "$message<hr><br>";

   print "</td></tr>";

}

print "</table>";



?>

Connect code


<?php



$db = mysql_connect("localhost", "MentorMenteeData", "mentormenteedata") or die("Could not connect.");

if(!$db)

	die("no db");

if(!mysql_select_db("mentormenteesystem",$db))

 	die("No database selected.");

if(!get_magic_quotes_gpc())

{

  $_GET = array_map('mysql_real_escape_string', $_GET);

  $_POST = array_map('mysql_real_escape_string', $_POST);

  $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);

}

else

{

   $_GET = array_map('stripslashes', $_GET);

   $_POST = array_map('stripslashes', $_POST);

   $_COOKIE = array_map('stripslashes', $_COOKIE);

   $_GET = array_map('mysql_real_escape_string', $_GET);

   $_POST = array_map('mysql_real_escape_string', $_POST);

   $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);

}



?>

Database Table:

-- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 09, 2012 at 10:05 AM
-- Server version: 5.5.16
-- PHP Version: 5.3.8


SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;


--
-- Database: `mentormenteesystem`
--

-- --------------------------------------------------------

--
-- Table structure for table `forum`
--

CREATE TABLE IF NOT EXISTS `elec_forum` (
  `postid` bigint(20) NOT NULL AUTO_INCREMENT,
  `author` varchar(255) NOT NULL DEFAULT '',
  `title` varchar(255) NOT NULL DEFAULT '',
  `post` mediumtext NOT NULL,
  `showtime` varchar(255) NOT NULL DEFAULT '',
 `realtime` bigint(20) NOT NULL DEFAULT '0',
 `lastposter` varchar(255) NOT NULL DEFAULT '',
 `numreplies` bigint(20) NOT NULL DEFAULT '0',
 `parentid` bigint(20) NOT NULL DEFAULT '0',
 `lastrepliedto` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`postid`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

Please guide and advise. I really do need a help here. thanks in advance

$updatepost="Update elec_forum set numreplies=numreplies+'1', lastposter='$name', [B][COLOR="#FF0000"]showtime='$displaytime1'[/COLOR][/B],lastrepliedto='$thedate' where postid='$id'"; 

Here you change the showtime of the original post (the part in red) when a reply is posted. If you don’t want that, don’t update the value of showtime.

Opps, sorry. It should be

showtime='$displaytime'

I do need to update the showtime, because I need the ‘Date and Time’ of the last poster (last person who reply to the thread) to be shown/updated in the index page (Page before entering the topic). But I just do not know why it updated the author ‘date and time’ as well.

And what column in your table would be containing the authore date and time?

I did not specified column for the author. In the above post, I have stated the database table I am using.
Do I really need to have the separate column for the author and the other poster?

:shifty: Ehm, if there is no column for the author date and time, then how can your script update that column?

Not for me :slight_smile:

What I actually mean is I use showtime column in database table for both topic author and the other poster.

Well, that’s the problem, isn’t it. If you use 1 column for 2 different things, and it can contain only 1. So I guess you’ll have to add a ‘last modified date’ column.

Thanks guido, I will try it and get back here if I still cant work it well.

Hi Guido,

I just realize I have the $getthreads[showtime] in the ‘Index code’. That variable is to display the posting time for the author post and the last poster(if there is reply). I will added ‘last modified date’ column into the database table and then use the ‘if else’ statement to select the column.

The ‘if else’ statement I plan to put in the ‘Index Code’ with condition of

if (numreplies==0)
showtime=‘$displaytime’
else
lastmodifieddate=‘$displaytime’

I am not sure, whether this is possible or not. Please correct me if I am wrong conceptually and technically.Thank you