Ok I would like to create a guestbook on my website. I want to use CGI, Do i need to make a database for the submitted text to be dislplayed? If so ... How? I can only use PHP or SQL for database
PHP and SQL are both languages, not databases. You'll have to check with your server for what database they do offer. Otherwise, you'll have to look into implementing your own.
database is mySQL Database
If you are writing your own guestbook script, you may find it easier to use a flat textfile database. That is, if you are new to CGI. Yes, MySQL is a million times more powerful than your ordinary textfile database, but it doesn't require the 'difficult' "language" for querrying a MySQL database.
i would love to be able to write a textfile database.. how do i do that?
open (FILEHANDLE, "path/to/file/database.dat");
print FILEHANDLE "data|you|want|saved";
This is the basic way to write to a textfile database with PERL. FILEHANDLE should be changed to what you want. Could be DATA, FILE, whatever you want. But each different database that you open should have a different filehandle. The path in the quotes and the filename tells the script the location of the text file you want to write to.
This file can be .txt, .dat, or for my own scripts I often use .cms (a file extension I created from my initials). As long as the file is capable of being written to, your script can write to it.
The second line tells the script what to write to the database. If you are familiar with databases, you have the database delimited by something. This will tell the script what pieces of info in the database are. These are just like field columns in MySQL (more or less). I like to use pipes "|" to delimit my fields. But you can use pretty much anything, but stay away from common strokes such as commas because if anyone types a comma in whatever their input is, you will have a messed up database. But a pipe is rarely used by people filling out forms whos info is saved to a database so they tend to be safe. Some people like double colons "::" If you have a database with users' usernames, passwords, and E-mail addresses and you want to save a new user's info to the database, you could have:
open (USERS, ">cgi-bin/profiler/users/userdatabase.dat");
print USERS "$username|$password|$email\n";
Of course you will have to set the scalars used in that sample to something which will probably be done with a sign up form (registration form where the users input the username and password they want and their E-mail address).
The > in the first line tells the script to save over the information already in the database. If you always want new info to be saved over the old and do not want the old info in there, just have the single > and the new info will replace the old.
But in the example above, you would obviously want to save all users' info to the database and not copy over the previous users info. So you should have >> instead of just one. This will tell the script to take the new user's info and append it to the bottom of the database. Which will keep the old information intact.
You can create a textfile on your harddrive called userdatabase.dat and upload it to your server and the script will automatically use it as the database. But if you run the script without uploading the database, most servers will automatically create the file.
This is just the tip of the iceberg. You will want to learn about file locking. if you are creating a database that a lot of people will be saving information to at the same time, it keeps the database's attention as it prints one person's info and then saves another person's or else you could have two peoples' information mixing into one line.
You will also need to learn the proper ways to draw the information from the database for use in your scripts.
Because you said (I think) that you know PHP, I can offer some suggestions for getting a similar result using PHP and MySQL. SQL can be somewhat hard to learn, but PHP is a good deal easier to use than PERL, from what I've foundm especially for a guestbook.
If you don't know much of MySQL, it might be a good idea to get a free script called phpMyAdmin, which lets you edit your database (for making it, setting up tables/columns, etc.) without (or with) using the SQL language. (http://www.phpmyadmin.net/)
However, you will need to learn SQL for accessing the data with PHP. These sites have some pretty good SQL tutorials:
After you learn SQL, it's just a matter of implementing it with PHP, and you can learn that by clicking on jeffmott's link, finding another online tutorial, or reading a PHP book that includes MySQL functions (many do).
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)