Home arrow MySQL arrow Page 3 - Building a Simple Affiliate System in PHP/MySQL

Being Common - MySQL

Setting up an affiliate system on your website can be very lucrative. While there are a lot of details involved, it need not be overly complicated. This article walks you through the steps of creating and setting up a basic affiliate system.

  1. Building a Simple Affiliate System in PHP/MySQL
  2. Building The Database
  3. Being Common
  4. Your Index Page
  5. Letting Affiliates Log in
  6. Adding Affiliate Code to Your Site
By: Roger Stringer
Rating: starstarstarstarstar / 58
November 10, 2005

print this article



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.

  $dbh=mysql_connect ($dbhost, $dbuser, $dbpassword) or die ('I cannot connect to the database because: ' . mysql_error());
  mysql_select_db ($dbname,$dbh);

  $affiliateForm = array(
      "username"=&gt;array("User Name","text",""),
      "site"=&gt;array("Your Web Site","text","http://"),
      "email"=&gt;array("Your Email","text","
      "ptype"=&gt;array("Payment Method","select","check|paypal"),
      "pemail"=&gt;array("Paypal Email","text",""),
      "address"=&gt;array("Your Address","textarea","")
  function makeKey($len){
        wxyz0123456789'; // characters to use in the password
      return $password;

  function AffiliateAdd(){
      $_POST['akey'] = makeKey(10);
      $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){
      $query = "UPDATE affiliates SET ";
      foreach($_POST as $field =&gt; $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(
      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:

  <title><?=$sitename?> Affiliate Center</title>
  <h1><?=$sitename?> Affiliate Center</h1>

Finally, we have "footer.php", this is the other half of the look and feel of your site:

  copyright 2005, <?=$sitename?>, <?=$siteurl?>

These can be changed quite easily, and changing them changes the whole look of the affiliate script.

>>> More MySQL Articles          >>> More By Roger Stringer

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates


Dev Shed Tutorial Topics: