You are invited to Log in or Register a free Frihost Account!

E-Cards with PHP & MySQL

So you want to make e-cards for your site, eh? I recently did this, and it took me a while to get it all working right, so here's a quick tutorial so you won't have to suffer what I did.
You'd think it would be a quick and easy sessions piece, right? This didn't work. I don't know exactly why, but even though I did session_destroy(), the session ID refused to go away. The problem this created was when a user sent one e-card and then tried to go back and create a new one. The new one wouldn't work because the error of a duplicate ID came up. What a bother.

First of all, I had to make the pictures. Obviously. Those are all displayed on one page, index.php, and here's their code:

<A HREF="makecard.php?image=imagename.jpg">

Naturally, each image has a different imagename. Now for makecard.php, where the user gets to put in his/her/its personal information. This is where I used sessions. In case you skipped that tutorial, here's the brief rundown:


Registering "image" means that the image the user chose will stick to that variable name until the session is destroyed. All the session information has to go before the HTML even starts. Now, the form:

<IMG SRC="<?echo $image;?>">
<form action="sendcard.php" method=GET>

Your Name:
<input type=text name=s_name size=25 maxlength=50>
Your Email Address:
<input type=text name=s_email size=25 maxlength=50><?escapeshellcmd($s_email);?>
Receiver's Name: <input type=text name=r_name size=25 maxlength=50>
Receiver's Email Address: <input type=text name=r_email size=25 maxlength=50>
Your Personal Message:<BR>
<TEXTAREA name=message cols=50 rows=5 wrap></TEXTAREA>

<input type=submit value="Send my card!">
<input type=reset value="Clear it">

If you know how to use forms, most of that should make sense immediately. escapeshellcmd() is a security feature so that malicious users can't put mySQL commands into the fields and get your information. If you want to know all the details, you can look that up in the manual.

One place I ran into a problem was if the user chose a picture, then changed his/her/its mind and went back. Because the image was a registered variable, the first image remained no matter what was chosen. This, fortunately, was an easy fix. At the top of the index.php page, I simply added:


Again, remember to put that before any of the HTML.

Now to send the card. The first step was to create a table inside mySQL. You can find great tutorials for that on devshed.
To give each card an individual ID, I used a function (which goes before the HTML, again), because as I mentioned, session_destroy() didn't appear to be destroying the ID itself, just the information within the session. If some of you have Core PHP Programming, you'll probably recognize this function... that book is my main resource.

function CreateID($length=16){
$Pool .= "1234567890floppydogs";
for($index = 0; $index < $length; $index++){
$sid .= substr($Pool,(rand()%(strlen($Pool))), 1);

Basically, that generates a random assortment, 16 chars long, made up of those chars I provided within $Pool.
Now to put everything into the mySQL table (and remember to keep your variables in order!). If there is already an entry with the same ID, mySQL will automatically return an error, in which case, the if(!result) will run, generating a new ID.

$result = mysql_query("INSERT INTO ecards VALUES('$sid', '$s_name', '$s_email','$r_name', '$r_email','$message', '$image')");
$result = mysql_query("INSERT INTO ecards VALUES('$sid', '$s_name', '$s_email','$r_name', '$r_email','$message', '$image')");

Now to mail the lucky recipient a message letting him/her/it know he/she/it has an e-card! And don't forge to destroy the session at the end so the user can go make another card if he/she/it wants to.

$mailTo = "$r_email";
$mailSubject = "You Have A FloppyDog!";
$mailHeader = "From: $s_name";
$message = "You lucky person, you! Someone has thought of you in a warm and fuzzy way and sent you a floppydog studios (TM) e-card! You can view this card at the following webpage: ";
$message .= "";
$message .= $sid;
mail($mailTo, $mailSubject, $message, $mailHeader);

That's it! Have fun e-carding, and if you want to see how it all works when put together.

Very Happy Very Happy Very Happy Very Happy
Related topics
HTTP AUTH with PHP and mySQL
Working with Excel, PHP & MySQL. Any Ideas
Ksiazki o PHP i mySQL, Helion
creating guestbooks (form) with php & mysql
A very good PHP MySQL Tutorial
Developing a Login System with PHP and MySQL
PHP and MySQL in windows
PHP and MYSQL on the computer
Simple php & mysql script
PHP & MySQL auth system
Help regarding Php & MySQL
which host do u like put php&mysql?
php and mysql form
PHP-Fusion | An Open Source CMS | PHP & MySQL Based
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.