/    Sign up×
Community /Pin to ProfileBookmark

PHP coupon code (I’m a newbie : )

I’m looking for help adding a coupon code box onto our existing webpage.
All I want to do is add a box that will subtract 10% from the subtotal if they have a valid coupon code. I’m not sure how simple or complex this might be. From looking around it looks like I need a “coupons” file that will have a list of the valid coupon codes and then I need to add the box that they will type in there code for validation. Also I would like to make it so once the code is used it is deemed invalid i.e. coupons can only be used once. Any and all help is appreciated. I’m a newbie with little to no html/php experience. To check out what the code is currently like go to: [url]http://www.oldcities.com[/url] . Just go to Shop Online and just add anything to the cart and click “Checkout via our secure server”… That will take you to the page that I would like to add the coupon box.

Thanks again

/josh

to post a comment
PHP

26 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsMar 16.2005 — a lot of times thoses carts were coded for php3, and the author(s) aren't smart enough to add comments to their scripts, which means a simple modification to those scripts becomes a major understaking

what cart is it? i'll have a look at it
Copy linkTweet thisAlerts:
@JMadrixauthorMar 16.2005 — I'm not sure what kind of cart or version of php he used. Our computer guy no longer works here. And I don't have much experience. It looks like he at one time had osCommerce installed but it doesn't look like it is using that right now.
Copy linkTweet thisAlerts:
@ShrineDesignsMar 16.2005 — "computer guy" lol

from the looks of thing i believe the cart you use is either custom made or modified

zip and email me the files

[email][email protected][/email]
Copy linkTweet thisAlerts:
@JMadrixauthorMar 16.2005 — Awesome.. Thanks for the help

I sent the .zip

It's coming from "[email protected]" just incase you have some mean filters or something. Should be all the files you need
Copy linkTweet thisAlerts:
@ShrineDesignsMar 17.2005 — i am suprised this script even works

- security holes all over

- coded around php3

it not worth my time and effor, to make a new script for free

sorry
Copy linkTweet thisAlerts:
@DJsACMar 17.2005 — If you have a mysql Database, and you can find the part of the script that calculates the total to be paid:

This is in the page after the form with coupon code textbox gets submitted.
[code=php]$couponcode = "2940230422"; // Get this value from a textbox somewhere.
$_SESSION['couponcode'] = $couponcode;[/code]


In script where total is calculated:
[code=php]$total = "102.30"; //some sum of money
$couponquery = "SELECT * FROM couponcodes WHERE code = '".$_SESSION['couponcode']."' LIMIT 1";
if(mysql_num_rows($couponquery) == 1)
{
$totaltopay = ($total * 0.9);
echo "Valid Coupon Code: Deducted 10% from Total.<br />"
$deletecoupon = mysql_query("DELETE FROM couponcodes WHERE code = '".$_SESSION['couponcode']."' LIMIT 1") or die(mysql_error());
}
else
{
$totaltopay = $total;
}
// continue with normal procedure...[/code]

Database contains a table called couponcode with at least 1 column ( called code).

You'll have to see If you can implement this yourself though...
Copy linkTweet thisAlerts:
@JMadrixauthorMar 17.2005 — Yes I do hav a MySQL database... I'm pretty sure I get what you are saying. So I need to make a "couponcode.php" somewhere right? With:

[code=php]

$couponcode = "2940230422"; // Get this value from a textbox somewhere.
$_SESSION['couponcode'] = $couponcode;
$couponcode = "2940230423"; // Get this value from a textbox somewhere.
$_SESSION['couponcode'] = $couponcode;
$couponcode = "2940230424"; // Get this value from a textbox somewhere.
$_SESSION['couponcode'] = $couponcode;
$couponcode = "2940230425"; // Get this value from a textbox somewhere.
$_SESSION['couponcode'] = $couponcode;

[/code]


So if I gave out 4 coupons they would be in there... Then when it's used it expires. Again sorry if I'm a total idiot...

I'll test it out and post the results...

Thanks for the help!
Copy linkTweet thisAlerts:
@DJsACMar 17.2005 — I would recommend putting the coupon codes in a table in the database instead of putting them into a text file.

A Database is probably going to be faster, requires less read/write permissions, and is easier to edit (phpmyadmin or alike)

The code should work as is, the biggest thing you have to figure out, is where to copy and paste it so that it gets processed correctly. ?

If you need an explanation of what a certain part needs just ask, but I thought it was pretty self explanatory as-is ?
Copy linkTweet thisAlerts:
@JMadrixauthorMar 17.2005 — Yeah I have phpMyAdmin and am having a hard time creating a database... I created a database. Then it takes me to create a table. (which I assume I'm supposed to do) But then there are alot of little boxes to fill which I'm not sure what needs to be what.

Field = coupon #?

Type = Text?

Length/Values = 10? (the number of characters in the coupon number?)

Attributes = binary?

Null = Null?

Default = no clue

extra = no clue

Primary, Index, or Unique?

Fulltext?

If nothing else this a very humbling experience :

I think I'll start on adding the code to the subtotal part first
Copy linkTweet thisAlerts:
@DJsACMar 17.2005 — ok. Create a table (like you were doing)

call the table: couponcodes

Make 2 FIELDS

call the first field: id

call the second field: code

type(id): double

type(code): double

Length(id): leave blank

Length(code): leave blank

Attributes(both): unsigned

Null(id): NO

Null(code): YES

Default(id): leave blank

Default(code: 0

Extra(id): auto_increment

Extra(code): leave blank

(id): Select Primary

(code): Select Unique

Click Create Table. It should work (of the top of my head ?)
Copy linkTweet thisAlerts:
@JMadrixauthorMar 17.2005 — Awesome got through that page! ?

I'm pretty sure I got the code you made pasted into the right place. Just a double-checking everything before I put it up on the site for a live test.

One last question.

Now how do I set the coupon codes? Do I "add index" in the table?

Thanks alot for all of your help. I know I'm a big dumbass if it comes to anything above simple HTML
Copy linkTweet thisAlerts:
@DJsACMar 17.2005 — if you click on the word 'couponcodes' in the menu on the left side of the phpmyadmin page, you'll see the 'structure' page. At the top of that page there's also a button that has "Insert"

Click on that and you'll get a bunch of textboxes.

Next to id select insert_id() and leave the textbox empty

next to code leave the selectionbox empty and put the code you want in the textbox.

Then click on GO. ?
Copy linkTweet thisAlerts:
@DJsACMar 17.2005 — ooh, I'm not sure whether you know this as wel

But on every page that uses "mysql_query" or mysql_ something you have to connect to the database.

to do that put this at or near the top of your page (before any of the mysql_ things ?)
[code=php]
<?php
//Connect to the Database
$DBHOST = "localhost"; //I'm assuming your database is on the same host, if not put the hostname here. (db23.host.net) etc..
$DBUSER = "USERNAME"; //Database's username
$DBPASS = "PASSWORD"; // database's password
$DB1 = "database_1"; // Database's NAME

$db = mysql_connect("$DBHOST","$DBUSER","$DBPASS");
mysql_select_db("$DB1",$db) or die(mysql_error()); //code which actually lets you connect, or gives a nice error explaining why something failed.
?>[/code]
Copy linkTweet thisAlerts:
@JMadrixauthorMar 17.2005 — Sweet dude! You rock... I've been trying to figure this stuff out for weeks and my boss was really starting to get pissed. I have added a couple different codes so looks like it's going to work. Looks like I can't add text though only numbers. But oh well... It will definately work for what we need it for.

I'm still a little unsure where to put

[code=php]


$couponcode = "2940230422"; // Get this value from a textbox somewhere.
$_SESSION['couponcode'] = $couponcode;


[/code]


Does that go on the page that has the text box where they enter it? Or on the page after that. Hence, they hit submit on the textbox page and the takes them to the "subtotal" page where they add their CC# info...
Copy linkTweet thisAlerts:
@DJsACMar 17.2005 — I expected the coupon code to contain only numbers sorry

If you want both numbers and letters

open PHPMYADMIN. click the table to go to the STRUCTURE page

select the code row and click the little "edit this row" button

There change DOUBLE to VARCHAR and in the LENGTH field you can put the number of characters your string is going to be (if your coupon code = 8 characters make the length 10 just in case etc. ?)

that way you'll be able to insert both Letters and numbers.
Copy linkTweet thisAlerts:
@DJsACMar 17.2005 — [code=php]$couponcode = "2940230422"; // Get this value from a textbox somewhere.
$_SESSION['couponcode'] = $couponcode;
[/code]


Does that go on the page that has the text box where they enter it? Or on the page after that. Hence, they hit submit on the textbox page and the takes them to the "subtotal" page where they add their CC# info... [/B][/QUOTE]

That goes on the page that the form submits to.

On the form there's a textbox with name="couponcode"

That code just says to save whatever is in the textbox as a global variable so we can use it just about anywhere ?

You'll actually need to use:
[code=php]$_SESSION['couponcode'] = $_POST['couponcode']; // Probably this one.
//OR
$_SESSION['couponcode'] = $_GET['couponcode']; // Might be this one
//OR
$_SESSION['couponcode'] = $_REQUEST['couponcode']; //if your not sure whether the form uses GET or POST, use this one, this should always work.[/code]
Copy linkTweet thisAlerts:
@JMadrixauthorMar 17.2005 — Awesome...

I'll stop bugging you now. Thanks again for all your help!
Copy linkTweet thisAlerts:
@DJsACMar 17.2005 — Glad to be of service ?

Usually I'm the one being a pain in the a$$ ?
Copy linkTweet thisAlerts:
@JMadrixauthorMar 17.2005 — hmm... Error.

Parse error: parse error, expecting ','' or ';'' in /usr/local/apache/htdocs/newstore/checkout2.php on line 108

I checked out line 108... It's:
[code=php]
$deletecoupon = mysql_query("DELETE FROM couponcodes WHERE code = '".$_SESSION['couponcode']."' LIMIT 1") or die(mysql_error());
[/code]


Any suggestions?
Copy linkTweet thisAlerts:
@JMadrixauthorMar 17.2005 — Wait I think I know what the problem is... Everything for the site is under a 'database1'... And for the coupons I made 'database2'... Maybe I should just make the 'couponcodes' table under 'database1'... Think that is the problem? Because it wouldn't let me connect to two different databases at once... :
Copy linkTweet thisAlerts:
@JMadrixauthorMar 17.2005 — Nope... Still get that parse error... I'm stuck.
Copy linkTweet thisAlerts:
@JMadrixauthorMar 17.2005 — Well got past that... Now I get another error! YAY! ?

[B]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /usr/local/apache/htdocs/newstore/checkout2.php on line 104 [/B]

No clue...

Any and all suggestions are wanted and appreciated...
Copy linkTweet thisAlerts:
@DJsACMar 17.2005 — you currently have:

$something = mysql_num_rows("Query here");

change that into:

$something = mysql_num_rows("Query here") or die(mysql_error());

that should at least give a better error message ?

that way maybe I can see what's going wrong ?
Copy linkTweet thisAlerts:
@JMadrixauthorMar 17.2005 — K... Added:
[code=php]
$sql_result = mysql_query($sql,$connection)
or die ("Couldn't get Cart");

$num_rows = mysql_num_rows($sql_result) or die(mysql_error());
if ($num_rows < 1) {
// echo "You have no Cart";
header("Location:index.php");
}
[/code]

Still get this:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /usr/local/apache/htdocs/newstore/checkout2.php on line 104
Copy linkTweet thisAlerts:
@DJsACMar 17.2005 — Do you happen to have MSN?

If so add me:

mauricea19995[AT]hotmail[dot]com

If not, could you email me the source code? or post that page? (or a larger part of it?)
Copy linkTweet thisAlerts:
@DJsACMar 17.2005 — Problem Solved. ?

Unless something else pops up... :rolleyes:
×

Success!

Help @JMadrix spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...