MySQL
  Home arrow MySQL arrow Page 3 - Building a Simple Affiliate System in PHP/MySQL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Building a Simple Affiliate System in PHP/MySQL
By: Roger Stringer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 47
    2005-11-10


    Table of Contents:
  • Building a Simple Affiliate System in PHP/MySQL
  • Building The Database
  • Being Common
  • Your Index Page
  • Letting Affiliates Log in
  • Adding Affiliate Code to Your Site

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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"=&gt;array("User Name","text",""),
          "password"=&gt;array("Password","password",""),
          "site"=&gt;array("Your Web Site","text","http://"),
          "email"=&gt;array("Your Email","text","
    yourname@yoursite.com"),
          "akey"=&gt;array("","hidden",""),
          "ptype"=&gt;array("Payment Method","select","check|paypal"),
          "pemail"=&gt;array("Paypal Email","text",""),
          "address"=&gt;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)&lt;$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 =&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(
            "total"=&gt;$total,
            "clicks"=&gt;$clicks,
            "orders"=&gt;$orders,
            "pending_total"=&gt;$pending_total,
            "paid_total"=&gt;$paid_total,
            "order_total"=&gt;$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.



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

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT