ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.


Building an advertising system with PHP, Part 2

(Page 3 out of 3)

Creating an admin panel

Let's have a look at creating a simple admin panel so the ads can be managed online, without having to use phpMyAdmin.

What our admin panel must be able to do are the following things:
- Add new advertisements
- Edit existing advertisements
- Delete existing advertisements

The easiest way of doing this is to include all three features in the same file, and that's exactly what we're going to do.

The code inserting a new advertisement looks like this:

// Add a new ad:
$link = ss($_POST['link']);
$image = ss($_POST['image']);
$adtype = ss($_POST['adtype']);
$priority = ss($_POST['priority']);

// Check everything, and insert new ad
if (!empty($link) AND !empty($image) AND !empty($adtype) AND !empty($priority)) {
        $db->query ("INSERT INTO ad (link, image, adtype, priority) VALUES ('$link', '$image', '$adtype', '$priority');");

And the code for updating an advertisement is quite similar:

// Update an ad
$id = intval($_GET['id']);

$link = ss($_POST['link']);
$image = ss($_POST['image']);
$adtype = ss($_POST['adtype']);
$priority = ss($_POST['priority']);

// Check everything, and update ad
if (!empty($link) AND !empty($image) AND !empty($adtype) AND !empty($priority)) {
        $db->query ("UPDATE ad SET link = '$link', image = '$image', adtype = '$adtype', priority = '$priority' WHERE adid = $id");

And the code deleting an advertisement looks like this:

// Delete an ad
$id = intval($_GET['id']);

$db->query ("DELETE FROM ad WHERE adid = $id");

Combine the above three code snippets, and what you get is the following: manage.php

You're probably wondering how the above code gives that result, but the above code is actually the most important and the other things I've added are merely sugar coating, like some HTML and design. Don't worry though, as you can download the full source code at the end of this article.


In this article I have shown you how to move our advertising system to a MySQL database, and we've added two new features. In addition, we've also created an ad management page. There are countless more things we can add, but I'll leave that as an exercise. Think about adding targeting features (e.g. detecting the user's language, and serving the right ad) or a new page where your advertisers can edit their own ads.

In the next and final part of this series we will extend our advertising system to collect statistics on each ad, like how many times has it been served, who has clicked, what the CTR (click-through-rate) is, etc. We'll be using PHP/SWF Charts to create really neat flash graphs.

If you have any comments on this article, head over to PHPit Forums or leave any comments below.

Click here to download the demo's and source code.

« Previous: Different Types & Priority Ads

2 Responses to “Building an advertising system with PHP, Part 2”

  1. PHPit - Totally PHP » Building an advertising system with PHP, Part 3 Says:

    […] Welcome to part 3 of the “Building an advertising system with PHP” series. In the previous parts (part 1 and part 2) I have shown you how to build your own advertising system using PHP and JavaScript. We’ve also added two extra features to our ad system and in part 2 we built a page to manage the ads as well. If you haven’t read either part yet, I highly recommend doing so before reading this part. […]

  2. Anup kumar singh Says:

    Thanks for this wonderful information, I am having only 6months of exp in PHP, I was assigned the advertisement task, from last 2days i was wondering how to create the advertisement module and i found this information this is very much informative and i will try to add more features to this system and will forward it to you.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Database Move
  2. Different Types & Priority Ads
  3. Management Page & Conclusion
Bookmark Article
Download Article
Download this article as a PDF file