Solved

Help needed checking if coupon code has been used

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
scan php uploads for viruses 5 23
PHP encrypted string and passing to a ASP Page 12 36
TypeError jquery issues with site - CSS conflicts 3 24
SQL to JSON 14 30
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
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.

680 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