This tutorial walkthrough is intended for those
who have a solid foundation of PHP basics.
This tutorial will teach you
how to setup the MySQL database and provide the code.
A walkthrough showing how to create your affiliation system.
Note: This tutorial walkthrough is intended for those who have a solid
foundation of PHP basics.
For this tutorial, all code is the full file with comments.
1.
Lets start off by creating our table.
Put the following code in the SQL
box in phpMyAdmin or similar script to create your database.
Code:
------------------------------------------------------------------------
CREATE TABLE `affiliates` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`banner` text NOT NULL,
`url` text NOT NULL,
`email` varchar(255) NOT NULL default '',
`in` int(11) NOT NULL default '0',
`out` int(11) NOT NULL default '0',
`active` int(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
------------------------------------------------------------------------
2. Now we need to create a config file to store the database access information.
(You will need to edit the username and password to your desired setting.)
Code:
------------------------------------------------------------------------
// connect.php for affiliate system
$host = "localhost"; // default
$user = "username"; // mysql username
$pass = "password"; // mysql password
$db = "database"; //mysql database
@mysql_connect($host,$user,$pass) or die("Could not connect
".mysql_error());
@mysql_select_db($db) or die("Could not connect to MySQL database $db");
?>
------------------------------------------------------------------------
3.
This is a fairly simple file, all it does is connect to the MySQL
database.
If the connection fails for any reason, the error will be
shown.
Your going to need a page where your site visitors can submit a request to become an affiliate.
Code:
------------------------------------------------------------------------
// include the connect.php
include "connect.php";
// add affiliate
$go = $_POST['add'];
if ($go)
{
// if the form is submitted, then process it
// htmlspecialchars strips html, and addslashes cancels out quotes
// get all the variables from the form
$name = htmlspecialchars(addslashes($_POST['name']));
$url = htmlspecialchars(addslashes($_POST['url']));
$image = htmlspecialchars(addslashes($_POST['image']));
$email = htmlspecialchars(addslashes($_POST['email']));
// insert the info into the database
mysql_query("INSERT INTO `affiliates` (`name`,`banner`,`url`,`email`,`active`) VALUES
('$name','$image','$url','$email','0');") or die("Insert Query Failed");
// query worked, tell user
echo "Your affiliate request has been submitted, but must be approved before it will show up on this site.
You will recieve an email once you are accepted.";
}
else
{
// if the form wasnt submitted
// show the form
echo "
Your Name:
Your Email:
Site URL:
Affiliate Image:
";
}
?>
|