Skip to content
Examination case 05

Product Information System

Product, price and stock records, with prices kept exact to two decimal places.

  • PHP
  • MySQL
  • PDO
  • HTML5
  • XAMPP
  • phpMyAdmin

Project overview

The Product Information System manages a store's product list: name, category, price and stock quantity. Staff can add products, review the catalog, update prices and stock levels, and remove discontinued items, with every price displayed to two decimal places.

Problem it solves

Inventory sheets often end up with prices like 12.999 or negative stock counts, which makes totals unreliable and hides real stock problems.

Key features

  • Add, list, view, edit and delete products
  • Prices stored as DECIMAL(10,2), so values stay exact
  • Prices accepted only when zero or more, with at most two decimals
  • Stock quantities limited to whole numbers of zero or more
  • Every price displayed with two decimal places

Case-specific rule

Exact prices and whole-number stock

Price must be zero or more with at most two decimal places, stock must be a whole number of zero or more, and prices are always shown with two decimals.

Database design

Database product_db, table products, InnoDB with utf8mb4.

Columns of the products table
ColumnTypeRule
idINT AUTO_INCREMENTPrimary key, generated automatically.
product_nameVARCHAR(100)Required. Up to 100 characters.
categoryVARCHAR(50)Required. Up to 50 characters.
priceDECIMAL(10,2)Required. Zero or more, with at most two decimal places.
stockINTRequired. A whole number of zero or more.

From the code

The validation behind this case, exactly as written in create.php.

product-system/create.php
if (!preg_match('/^(?:0|[1-9][0-9]*)(?:\.[0-9]{1,2})?$/', $values['price'])) {
    $errors[] = 'Price must be zero or greater and have at most two decimal places.';
}
if (!preg_match('/^(0|[1-9][0-9]*)$/', $values['stock'])) {
    $errors[] = 'Stock must be a whole number greater than or equal to zero.';
}

Challenges and solutions

01

Money without rounding surprises

Challenge encountered

Floating-point numbers cannot store most decimal prices exactly, and free-text prices invite values like 12.999.

Solution implemented

Prices must match a pattern allowing at most two decimals, are stored in a DECIMAL(10,2) column, and are displayed with number_format to two decimals.

02

Stock that cannot go negative

Challenge encountered

A typo in the stock field could save a negative or fractional quantity.

Solution implemented

Stock is checked against a whole-number pattern on the server, so only values of zero or more are accepted.

Outcome and impact

Accurate, consistently formatted product records ready for reporting, with price and stock errors stopped at the form.

Every PHP file in the project passed syntax checking before submission.

Files delivered
8
PHP pages
6
Table columns
5
Sample records
3

Project files

Delivered in the required eight-file format, ready to drop into XAMPP.

  • config.phpOpens the PDO connection to the database, with exceptions turned on for errors.
  • index.phpLists every record, newest first, with View, Edit and Delete links on each row.
  • create.phpValidates a new record, saves it with a prepared INSERT, then redirects to the list.
  • view.phpShows one record after checking that its ID is a positive whole number.
  • edit.phpLoads an existing record, validates the changes and saves them with a prepared UPDATE.
  • delete.phpShows a confirmation page, then deletes the record only on a confirmed POST.
  • database.sqlCreates the database and table, then inserts three fictional sample records.
  • README.mdSetup steps for XAMPP and phpMyAdmin, plus the feature list and local URL.

Secure by default

The same six practices protect all five examination cases.

  • Prepared statementsEvery query runs through PDO prepared statements with named placeholders, so typed input never becomes part of the SQL.
  • Escaped outputEvery value passes through htmlspecialchars with ENT_QUOTES and UTF-8 before it reaches the page, which blocks script injection.
  • POST-only changesCreating, editing and deleting only happen on POST requests, and a delete needs an explicit confirmation first.
  • Safe record IDsIDs must be positive whole numbers. A missing or invalid record returns a 404 with a clear Record not found page.
  • Post, redirect, getAfter a successful save the page redirects to the list, so refreshing the browser never submits the form twice.
  • Helpful validationFailed saves list every problem at once and keep what was typed, so nobody has to fill in the form again.

See it working

Add, edit and delete records in the live demo, and watch each prepared statement as it runs.

Open live demo