Solved

Help needed checking if coupon code has been used

Posted on 2014-03-20
2
674 Views
Last Modified: 2014-03-25
I'm trying to check if a coupon code is valid and has not been used. My first step is to check the couponCode value against the registeredCodesAr array to make sure the code exists. If the code exists, I then want to make sure it has not been used by looking for it in the table.

The table I'm testing against has code01 and codepm01, but not codepm02.

Unfortunately though, it doesn't seem to be working. I've narrowed down the problem to my PHP, and was hoping someone might be able to see where my mistake is.

paycode.php
$registeredCodesAr = array('Code01', 'CodePM01', 'CodePM02');
$registeredCodes = array_map('strtolower', $registeredCodesAr);

$requestedCode  = mysql_real_escape_string(htmlentities(strtolower($_REQUEST['couponCode'])));

if( in_array($requestedCode, $registeredCodes) ){

	$result = mysql_query("SELECT couponCode FROM requests WHERE couponCode = $requestedCode");

	if(mysql_num_rows($result) > 0) {
     	echo 'false'; // Code has been used
	} else {
   		echo 'true'; // Code has not been used
	}
} else {
   		echo 'true'; // Code doesn't exist
}

Open in new window

Portion of HTML/Script
<script type="text/javascript">

$(document).ready(function() {

	$("#frm_contact").validate({
		rules: {
			couponCode: {
				required: true,
				remote: "paypal-express-checkout/paycode.php",
			}					
		},
		messages: {
			couponCode: "This code has been used."
		}		
	});

});
</script>
<form name="frm_contact" id="frm_contact" method="post" action="">
<input type="text" class="ft" name="couponCode" id="couponCode" />
</form>

Open in new window

0
Comment
Question by:jej07
2 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 39944082
If you're new to PHP and want to get a foundation in how all of this works, you might find some good learning resources here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

The general strategy would be to keep a data base table of coupon codes.  When each code was used, you would remove it from the table.  A query that looked for the coupon code in the table would return either one or zero rows.  If there is a row, the coupon code has not been used.  The column for the coupon code would probably be marked UNIQUE to prevent duplicate codes from accidentally "giving away the store."  This is an area where marketing, merchandising and business rules determine the technical solution.  Some promotions would be loss leaders with "one coupon per customer" and some would be "clearance" to encourage as many sales as possible.

You might also have an alias name for the coupon codes (not unique) that would allow you to associate the coupons with a particular marketing campaign.  It would say something like "50 cents off orange juice."  This would be the thing that the human customer would see and associate with the promotion.  The coupon code would be a unique number, barcoded into the coupon document.

You're going to be facing a data base conversion soon.  This article explains why and what you must do to keep your web applications running.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 100 total points
ID: 39944501
Agree with Ray.
That said, here guidelines to post question here at EE: you should specify what does it mean 'it doesn't seem to be working': do you receive any error message? What is the output of your script? Alway true? Always false?
Please, place at the top of php script these two lines

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');

And post here any error message you get. Be more descriptive about 'not working' actuallly means so we can figur out where is the problem.

Finally: your php script returns true even if the code doesn't exist: why? IMHO, the logic shouldb:

if code doesn't exist -> false
if code has been used yet -> false
else -> true
$registeredCodesAr = array('Code01', 'CodePM01', 'CodePM02');
$registeredCodes = array_map('strtolower', $registeredCodesAr);

$requestedCode  = mysql_real_escape_string(htmlentities(strtolower($_REQUEST['couponCode'])));

if( in_array($requestedCode, $registeredCodes) ){

	$result = mysql_query("SELECT couponCode FROM requests WHERE couponCode = $requestedCode");

	if(mysql_num_rows($result) > 0) {
     	echo 'false'; // Code has been used
	} else {
   		echo 'true'; // Code has not been used
	}
} else {
   		echo 'false'; // Code doesn't exist
}

Open in new window


Cheers
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Tool to Build Android and iOS App 3 54
updating table data with inner join 9 24
Before I get too far.. best way to save dates data 4 16
tag title an image 13 24
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now