Solved

Help needed checking if coupon code has been used

Posted on 2014-03-20
2
660 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 30

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

705 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

17 Experts available now in Live!

Get 1:1 Help Now