Lesson 80: Preventing Duplicate Auctions for the Same Listing with Database-Level Validation

Introduction

As the Flipnzee Auctions plugin continued to mature, most of the auction workflow had become stable. Earlier lessons introduced automatic auction closing, winner detection, transaction generation, payment management, and duplicate transaction protection.

While reviewing historical auction data, another important question arose:

Can a listing accidentally have more than one active auction?

Although previous improvements had already reduced the possibility of duplicate auctions, adding another layer of protection inside the Auction Manager would make the plugin even more reliable.

This lesson focuses on auditing the auction creation workflow and implementing a final database-level safeguard that prevents multiple active auctions from being created for the same listing.


Why This Improvement Matters

A marketplace should never allow confusion about which auction is currently valid.

Without proper validation, multiple active auctions for the same listing could cause:

  • Multiple bidding interfaces
  • Conflicting highest bids
  • Incorrect winner selection
  • Duplicate transactions
  • Difficult ownership transfer

Even if such situations only occur because of programming mistakes or repeated requests, preventing them is essential.


Current Workflow Review

Before making changes, the existing auction lifecycle should be reviewed.

Listing Created
        │
        ▼
Create Auction
        │
        ▼
Auction Starts
        │
        ▼
Users Place Bids
        │
        ▼
Auction Ends
        │
        ▼
Winner Selected
        │
        ▼
Transaction Created

The only missing safeguard is ensuring that only one active auction can exist for a listing at any given time.


Objectives

In this lesson we will:

  • Audit the auction creation logic.
  • Search where auctions are inserted into the database.
  • Check whether an active auction already exists.
  • Prevent duplicate active auctions.
  • Return the existing auction instead of creating another one.
  • Test the protection with multiple creation attempts.

Implementation Plan

Step 1

Locate the auction creation method.

Search for:

create_auction(

or

$wpdb->insert(

inside

includes/class-auction-manager.php

Step 2

Before inserting a new auction, search for an existing active auction belonging to the same listing.

The validation should resemble:

SELECT id
FROM wp_flipnzee_auctions
WHERE listing_id = ?
AND status = 'active'
LIMIT 1

Step 3

If an active auction exists:

  • do not insert another record
  • return the existing auction ID

Step 4

Only if no active auction exists should the plugin execute:

$wpdb->insert(...)

Step 5

Test using:

  • Add Auction page
  • Edit Auction page
  • phpMyAdmin
  • Frontend listing page

Expected Workflow After Improvement

Create Auction
        │
        ▼
Check Active Auction
        │
   Exists?
    │       │
   Yes      No
    │        │
Return ID  Create Auction

What We Will Learn

This lesson introduces another important software engineering principle:

  • Defensive programming
  • Database validation
  • Idempotent creation methods
  • Marketplace integrity
  • Multi-layer validation
  • Business rule enforcement

Files Expected to Change

Primary file:

includes/class-auction-manager.php

Possible testing files:

admin/class-admin-add-auction.php

admin/class-admin-edit-auction.php

Expected Outcome

After completing Lesson 80:

  • A listing can never have two active auctions.
  • Duplicate auction creation attempts become harmless.
  • Historical auction records remain preserved.
  • Marketplace integrity improves.
  • The auction lifecycle becomes even more robust.

Difficulty

Intermediate


Estimated Time

30–45 minutes


Next Lesson Preview

Lesson 81 – Automatically Archive Completed Auctions and Preserve Historical Records

We’ll enhance the auction lifecycle by introducing an archival mechanism so completed auctions are retained for reporting and auditing while keeping active auction data clean and efficient.

Leave a Reply