Skip to content

Instantly share code, notes, and snippets.

@exaland
Last active January 14, 2023 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exaland/d04d9e43f6ca6eddcf2f36ab7e55bd40 to your computer and use it in GitHub Desktop.
Save exaland/d04d9e43f6ca6eddcf2f36ab7e55bd40 to your computer and use it in GitHub Desktop.
Create an Redeem Card in PHP
Create a database table to store the gift card information, such as the card number, pin, balance, expiration date, and status (active or redeemed).
Copy code
CREATE TABLE gift_cards (
id INT AUTO_INCREMENT PRIMARY KEY,
card_number VARCHAR(16) NOT NULL,
pin VARCHAR(4) NOT NULL,
balance DECIMAL(10,2) NOT NULL,
expiration_date DATE NOT NULL,
status ENUM('active', 'redeemed') NOT NULL
);
Create a form for customers to enter the gift card number and pin to redeem the card.
<form action="redeem.php" method="post">
<label for="card_number">Card Number:</label>
<input type="text" id="card_number" name="card_number">
<label for="pin">Pin:</label>
<input type="password" id="pin" name="pin">
<input type="submit" value="Redeem">
</form>
Write PHP code to validate the gift card information entered by the customer and check the card's status and balance.
Copy code
<?php
//connect to the database
$db = new mysqli('host', 'username', 'password', 'database');
//get the form data
$card_number = $db->real_escape_string($_POST['card_number']);
$pin = $db->real_escape_string($_POST['pin']);
//check if the card number and pin match a row in the database
$query = "SELECT id, balance, status FROM gift_cards WHERE card_number='$card_number' AND pin='$pin'";
$result = $db->query($query);
if($result->num_rows == 1){
$card = $result->fetch_assoc();
if($card['status'] == 'active'){
//redeem the card
$balance = $card['balance'];
$id = $card['id'];
$query = "UPDATE gift_cards SET status='redeemed' WHERE id=$id";
$db->query($query);
echo "Card redeemed! Your remaining balance is $balance.";
} else {
echo "Card has already been redeemed.";
}
} else {
echo "Invalid card number or pin.";
}
$db->close();
This is a basic example, some important security measure should be added, like prepared statement, validation against xss, sql injection, and other security vulnerabilities.
Please note that this code is for demonstration purposes only and should not be used in a production environment without proper testing and security measures in place.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment