Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: jam14online
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

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!

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


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.

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!

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.

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!

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.

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

![]() |
![]() |
![]() |

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