Implementing Member Level Points Redemption in a PHP E‑Commerce Site
This tutorial explains how to create a MySQL members table, configure a PHP database connection, and build backend scripts and a web form that allow e‑commerce users to exchange points for membership level upgrades, complete with full code examples.
With the rapid growth of e‑commerce, online malls need features that boost user engagement, such as a member level points redemption system. This article shows how to develop that functionality using PHP.
First, create a MySQL table named members with the fields id (auto‑increment primary key), name (VARCHAR(50)), level (INT), and points (INT) to store member information.
Next, add a config.php file in the project root to hold the database connection settings. The file contains:
<?php
$db_host = 'localhost';
$db_username = 'username';
$db_password = 'password';
$db_name = 'database_name';
$conn = mysqli_connect($db_host, $db_username, $db_password, $db_name);
if (!$conn) {
die('Database connection failed: ' . mysqli_connect_error());
}
?>Replace username , password , and database_name with your actual credentials.
Then create redeem_points.php to process the redemption logic. The script starts a session, reads the member ID from the session, and obtains the points and target level from $_POST . It updates the member record with an SQL UPDATE statement:
<?php
session_start();
require 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$member_id = $_SESSION['member_id'];
$points = $_POST['points'];
$level = $_POST['level'];
$sql = "UPDATE members SET points = points - $points, level = $level WHERE id = $member_id";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "Member level points redemption successful!";
} else {
echo "Member level points redemption failed: " . mysqli_error($conn);
}
}
?>The script uses session_start() to access the logged‑in member ID, reads the submitted data from $_POST , and runs the UPDATE query to adjust points and level.
Finally, create a simple HTML page (also named redeem_points.php ) that presents a form for members to enter the points to exchange and the desired level:
<?php require 'config.php'; ?>
<!DOCTYPE html>
<html>
<head>
<title>Member Level Points Redemption</title>
</head>
<body>
<h1>Member Level Points Redemption</h1>
<form method="POST" action="redeem_points.php">
<label for="points">Points:</label>
<input type="text" id="points" name="points"><br>
<label for="level">Level:</label>
<input type="text" id="level" name="level"><br>
<input type="submit" value="Redeem">
</form>
</body>
</html>The page includes the configuration file, displays a form, and submits the data to the same script for processing.
By following these steps, you have a complete member level points redemption feature that lets e‑commerce users exchange points for higher membership levels, encouraging more purchases and increasing user retention.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.