Computing.Net > Forums > Web Development > Simple PHP Guest Book

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Simple PHP Guest Book

Reply to Message Icon

Name: jam14online
Date: April 11, 2004 at 00:14:37 Pacific
OS: Microsoft Windows XP Prof
CPU/Ram: AMD Athlon XP 1700+/384MB
Comment:

Hi everyone,

I have begun to write a simple script for a guest book as I want to improve my PHP skills a bit. Right now, it's rather simplistic in that it does not use a MySQL database. I guess if I did connect to a database it would actually be simpler.

If you download this file you can see how much I've done. I've got Apache 1.3 and PHP 4.3 installed on this machine and I've been using it as my testbed.

However, I always get the following error:

Parse error: parse error, unexpected T_VARIABLE in c:\program files\apache group\apache\htdocs\guestbook\post.php on line 11

Please download my script and help me! I'm pretty sure it's a very stupid error created by me.

Thanks in advance,

James




Sponsored Link
Ads by Google

Response Number 1
Name: Khalid
Date: April 11, 2004 at 00:54:52 Pacific
Reply:

Line 11 in post.php should be:

$addition = "

$comments</p><address>$name</address>";

Dont use the '+'-sign to add strings to another but use the '.'-sign. But in this case you dont need to do that all, just use double-quotes (") and type the variables directly in the string.
Good luck!


0

Response Number 2
Name: jam14online
Date: April 11, 2004 at 01:00:20 Pacific
Reply:

Thank you so much Khalid for the speedy response!

I've changed line 11 in post.php as you said and it now works. However, it just overwrites the contents of the file. It doesn't just add it to the beginning.

Why is this?

Thanks again,

James



0

Response Number 3
Name: jam14online
Date: April 11, 2004 at 01:32:20 Pacific
Reply:

Actually, I've got it to work now. I rewrote the script after reading a tutorial. The only problem now is that it adds stuff to the end of the file, but not the beginning!

I don't mind it too much but I prefer the most recent entry to be at the top. I've uploaded a newer file which you can



0

Response Number 4
Name: jam14online
Date: April 11, 2004 at 01:33:32 Pacific
Reply:

(don't know what happened there)... download from here.



0

Response Number 5
Name: Khalid
Date: April 11, 2004 at 02:02:37 Pacific
Reply:

Open post.php and use the following code:

//Add markup to beginning of file
rewind($handle);
$write = fputs($handle, $addition);

As you can see I've added a rewind-function. It takes a file handle and resets the file position indicator to the beginning of the file.

Hope this helps.


0

Related Posts

See More



Response Number 6
Name: jam14online
Date: April 11, 2004 at 05:16:27 Pacific
Reply:

Thank you so much Khalid for you help. I now have the guest book up and running. Please be the first (real) person to sign it!

Once again, thank you so much for your help!



0

Response Number 7
Name: Khalid
Date: April 11, 2004 at 06:13:33 Pacific
Reply:

I saw on your site some things were not working properly. Perhaps you can use this code:

----------
//Opens file for reading and writing
//Also puts pointer at beginning of file
$handle = fopen("comments.txt", "r+");

//Put contents of file in a variable
$contents = fread($handle, filesize("comments.txt"));

//Add markup to beginning of file
rewind($handle);
$write = fputs($handle, $addition);
$write = fputs($handle, $contents);
----------

This code will add the comments to the top of the file comments.txt. If the file becomes very large, this script will slow down but for a simple guestbook like yours this isnt a big problem.


0

Response Number 8
Name: jam14online
Date: April 11, 2004 at 06:21:52 Pacific
Reply:

OK, but wouldn't that add two sets of comments, i.e. one from the $addition variable and one from $comments? I was really stupid in that I forgot to chmod the comments.txt file to 777 when I overwrote it!

Please explain further how the above code will work. Thanks again,

James

P.S. Don't forget to sign my guest book!



0

Response Number 9
Name: Khalid
Date: April 11, 2004 at 07:15:17 Pacific
Reply:

The code first reads the content of the file 'comments.txt' and stores it in a variable called '$contents'. Then it overwrite the file 'comments.txt' with the variable '$addition' and adds the variable '$contents' at the end of the file.
So you now have all the origal comments plus the new comment at the top of the file.


0

Response Number 10
Name: jam14online
Date: April 11, 2004 at 07:41:31 Pacific
Reply:

OK, I understand now. Very neat. I'm going to try it a bit later; I'll post to the guest book about it when I've configured it.

BTW, thanks for posting! ;)

Thanks for all the help,

James


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Simple PHP Guest Book

Tiny guest book script www.computing.net/answers/webdevel/tiny-guest-book-script/415.html

Guest Book www.computing.net/answers/webdevel/guest-book/2930.html

Simple PHP Validation script www.computing.net/answers/webdevel/simple-php-validation-script/2729.html