Building a Simple Affiliate System in PHP/MySQL - Being Common (
Page 3 of 6 )
Now, we're also going to create a file called "common.php." This file is very important as it contains functions used by the script.
This script is being built so that the common file is the brains of the whole system.
<?
session_start();
include("config.php");
$dbh=mysql_connect ($dbhost, $dbuser, $dbpassword) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbname,$dbh);
define("DBH",$dbh);
$affiliateForm = array(
"username"=>array("User Name","text",""),
"password"=>array("Password","password",""),
"site"=>array("Your Web Site","text","http://"),
"email"=>array("Your Email","text","yourname@yoursite.com"),
"akey"=>array("","hidden",""),
"ptype"=>array("Payment Method","select","check|paypal"),
"pemail"=>array("Paypal Email","text",""),
"address"=>array("Your Address","textarea","")
);
function makeKey($len){
$chars=
'ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuv
wxyz0123456789'; // characters to use in the password
mt_srand((double)microtime()*1000000^getmypid());
while(strlen($password)<$len){
$password.=substr($chars,(mt_rand()%strlen($chars)),1);
}
return $password;
}
function AffiliateAdd(){
$_POST['akey'] = makeKey(10);
unset($_POST['ID']);
unset($_POST['step']);
$query = "INSERT INTO affiliates (".implode(", ",array_keys($_POST)).") VALUES ('".implode("', '",array_map("mysql_real_escape_string",$_POST))."')";
mysql_query($query,DBH) or die( mysql_error() );
return mysql_insert_id();
}
function AffiliateUpdate($aid){
unset($_POST['step']);
$query = "UPDATE affiliates SET ";
foreach($_POST as $field => $value) {
$query .= "$field = \"".mysql_real_escape_string($value)."\", ";
}
$query = substr($query, 0, strlen($query)-2)." WHERE ID = '{$aid}'";
mysql_query($query,DBH) or die( mysql_error() );
}
function AffiliateStats($aid=""){
$sql = "SELECT * FROM affiliates WHERE ID='{$aid}'";
$lo = mysql_query( $sql );
$affi = mysql_fetch_assoc($lo);
list($clicks) = mysql_fetch_row( mysql_query("SELECT COUNT(*) FROM affstats WHERE akey= '{$affi['akey']}'") );
list($orders) = mysql_fetch_row( mysql_query("SELECT COUNT(*) FROM affstats WHERE akey= '{$affi['akey']}' AND type=2") );
list($pending_total) = mysql_fetch_row( mysql_query("SELECT SUM(price) FROM affstats WHERE akey= '{$affi['akey']}' AND type=2 AND paid=0") );
list($paid_total) = mysql_fetch_row( mysql_query("SELECT SUM(price) FROM affstats WHERE akey= '{$affi['akey']}' AND type=2 AND paid=1") );
$total = $clicks+$orders;
$order_total = $pending_total+$paid_total;
$return = array(
"total"=>$total,
"clicks"=>$clicks,
"orders"=>$orders,
"pending_total"=>$pending_total,
"paid_total"=>$paid_total,
"order_total"=>$order_total
);
return $return;
}
?>
Most of these functions are pretty straightforward; you can figure them out just from their names.
Look And Feel
There are two files that need to be created to give the site its look and feel.
The first one is "header.php." This is the header for the site; change it to integrate more with your existing site:
<html>
<head>
<title><?=$sitename?> Affiliate Center</title>
</head>
<body>
<h1><?=$sitename?> Affiliate Center</h1>
Finally, we have "footer.php", this is the other half of the look and feel of your site:
<hr/>
copyright 2005, <?=$sitename?>, <?=$siteurl?>
These can be changed quite easily, and changing them changes the whole look of the affiliate script.
| | Discuss Building a Simple Affiliate System in PHP/MySQL | | | | | | | When I saw the headline, I was quite anxious to read this article. Unfortunately,... | | | | | | We are sorry to hear that you found the article disappointing. I am contacting the... | | | | | | Well, The article is about building a simple affiliate system. Which is what it... | | | | | | Hi. DevShed is all about open source programming. This article explores the... | | | | | | This article was entirely appropriate. I mean if it was about inventory tracking... | | | | | | Hello All,
A big thanks to the author for taking time to write this... | | | | | | There was a slight bad there when they inserted the article into the system... | | | | | | Thanks! I'll let ya know how I make out!
MrsDelley | | | | | | and what is this:
Parse error: parse error, unexpected ';' in... | | | | | | sorry i am blind today, although it is an error.
for those who may ask this Q... | | | | | | one more Qurestion i have;
What is this: <img src="/affiliates/sale.php">.... | | | | | | I keep getting this error message:
Warning: mysql_query(): supplied argument is... | | | | | | hi, first of all thanks for sharing this nice script. it seems to do exactly what i... | | | | | | scrap my last comment, i should be logging into manage.php not accesscotrol.php.... | | | | | | Its a very nice script.:)
Some notes from the comments that could be updated to... | | | | | | I get the same exact error message. Could someone shed some light on the solution... | | | | | | I get alot of these errors:
Warning: mysql_query(): supplied argument is not a... | | | | | | Hi There,
this script in common.php:
list($pending_total) = mysql_fetch_row(... | | | | | | I have gone through each page and created all the files. Needless to say, there is a... | | | | | | You access your MySQL database through a program called PHPMyAdmin. It should be... | | | | | | Hi to the Poster above,
I did eventually figure out how to upload the tables as... | | | | | | Hi, I'm have 2 problems at the moment with the system, although I see it will be... | | | | | | These scripts are broken and make a mess. | | | | | | >>> Post your comment now! | | | | | |
|
 |