Some guestbook problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kilgoretrout
Forum Newbie
Posts: 4
Joined: Sun Oct 17, 2010 12:47 pm

Some guestbook problems

Post by kilgoretrout »

Hey!

First off, i'm a php and mysql n00b trying to create an guestbook. My first problem is that the time shown in every post says 00:00:00, the date works.
Second problem is i'm trying to implement this CAPTCHA http://www.white-hat-web-design.co.uk/a ... aptcha.php with no problem getting the captcha-image and text-input to show, but can't get the code that will validate the text-input when clicking the submit button to work with my own code.
Any suggestions?

My single file guestbook without CAPTCHA:

Code: Select all

<?php
//connect to the database
$connect = mysql_connect ("my host","user","pass") or die("Error connecting to db");
//select table
mysql_select_db("my db") or die("Error selecting db");

echo "<head>
<link href='style.css' rel='stylesheet' type='text/css'>
</head>
<body leftmargin='20' topmargin='15'>
";

if ($_POST['submit'])
{

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$date = date("y-m-d");
$time = time("H:i:s");

if ($name&&$message)
{

$querypost = mysql_query("INSERT INTO guestbook VALUES ('','$name','$email','$message','$date','$time')");
echo "Please wait... <meta http-equiv='refresh' content='2'>";

}
else
   echo "Please fill out both Name and Comment fields!";
}

echo "
<table width='100%'>
 <tr>
 <td class='borderTabell2'>

<form action='index.php' method='POST'>
<table width='100%' cellspacing='1' cellpadding='2'>
    <tr>
      <td width='25%'>
      <font size='2'>Name:</font>
      </td>
      <td>
      <input type='text' size='55' name='name' maxlength='50'>
      </td>
     </tr>
     
     <tr>
      <td>
      <font size='2'>Email:</font>
      </td>
      <td>
      <input type='text' size='55' name='email' maxlength='50'>
      </td>
     </tr>


     <tr>
      <td valign='top'>
      <font size='2'>Comment:</font>
      </td>
      <td>
      <textarea cols='40' rows='5' name='message' maxlength='250'></textarea>
      </td>
     </tr>
     
     <tr>
      <td>
      
      </td>
      <td align='center'>
      <input type='submit' name='submit' value='Post'>
      </td>
     </tr>

</table>
</form>

</td>
     </tr>

</table>



</body>
";

echo "<table border='0'>
<tr>
<td height='20'>
</td>
</tr>
</table>";



//use query to get ALL data
$queryget = mysql_query("SELECT * FROM guestbook ORDER BY `id` DESC") or die("Error with query");

while ($row = mysql_fetch_assoc($queryget))
{
 //get row data and store in vars
 $id = $row['id'];
 $name = $row['name'];
 $email = $row['email'];
 $message = $row['message'];
 $date = $row['date'];
 $time = $row['time'];
 
 // show data to user
 echo"
 <table width='100%'>
 <tr>
 <td class='borderTabell2'>
 
 <table width='100%' cellspacing='1' cellpadding='2'>
    
    <tr>
      <td width='50%'>
      <font size='1'><b>#$id</b></font>
      </td>
      <td width='50%' align='right'>
      <font size='1'><b>$date $time</b></font>
      </td>
    </tr>
     
    <tr>
      <td colspan='2'>
      <font size='1'><b>Name: $name </b></font>
      </td>
    </tr>
    
    <tr>
      <td colspan='2'>
     <font size='2'> ".nl2br(strip_tags($message))." </font>
      </td>
    </tr>
 </table>
 
 </td>
    </tr>
 </table>
 
 ";
 
}



?>
The CAPTCHA code i need to implement:

Code: Select all

<?php 
session_start();

if( isset($_POST['submit'])) {
   if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
		// Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
		echo 'Thank you. Your message said "'.$_POST['message'].'"';
		unset($_SESSION['security_code']);
   } else {
		// Insert your code for showing an error message here
		echo 'Sorry, you have provided an invalid security code';
   }
} else {
?>
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Some guestbook problems

Post by twinedev »

In regards to the time, you need to do date('H:i:s"). time() just returns current time stamp, date() formats them.

-Greg
kilgoretrout
Forum Newbie
Posts: 4
Joined: Sun Oct 17, 2010 12:47 pm

Re: Some guestbook problems

Post by kilgoretrout »

twinedev wrote:In regards to the time, you need to do date('H:i:s"). time() just returns current time stamp, date() formats them.

-Greg
Thanks! Time working as it should now.
kilgoretrout
Forum Newbie
Posts: 4
Joined: Sun Oct 17, 2010 12:47 pm

Re: Some guestbook problems

Post by kilgoretrout »

Nevermind.....solved it.
Sephern
Forum Commoner
Posts: 73
Joined: Sun Jan 04, 2009 4:44 pm

Re: Some guestbook problems

Post by Sephern »

Just a word of advice, it'd probably be in your interests to store the date and time as some form of timestamp, as opposed to actual values.

Where at the moment, you probably have 2 fields (date and time), which you store in a set format (dd-mm-yyyy, hh:mm for example), you can store one timestamp integer.

There are 2 distinct advantages to this.

The first is space. You can either store the value as a MySQL DateTime type, or as a unix timestamp. Either way you're going to use significantly less space in the database as opposed to storing them as pre-processed strings.

The second is flexibility. At the moment, you might want to output the date like this
17-10-2010
What about if in the future, you want to output it like this
Sunday the 17th of October, 2010.

If you're storing static strings you'll need to convert it every time you load it from the database, and then restructure it using the date() function. The alternative is just to store the output from time(), and then format it as appropriate when its fetched from the database.

The third benefit, though it may not suit you at the moment, would be the querying and logical sorting of the data. If you wanted to get everything between 2 set dates for example, you could do so easily by getting the timestamps between those values in one SQL query. It'd be even easier if you use the DATETIME type. ;)
kilgoretrout
Forum Newbie
Posts: 4
Joined: Sun Oct 17, 2010 12:47 pm

Re: Some guestbook problems

Post by kilgoretrout »

@Sephern
Yeah i made the date row DATETIME and deleted time, mainly because it wouldn't work any other way. But apparently it was a wise move for lots of other reasons aswell :)
Thanks for the advice!
Post Reply