Solved

Help needed checking if coupon code has been used

Posted on 2014-03-20
2
694 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 110

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
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 is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

738 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