Red100 Posted January 14, 2016 Share Posted January 14, 2016 Hi, I am new to php and really need some help & advise. I am using a basic php script where a customer adds a promo code in a form which then redirects them to a special ordering page. We have a larger promotion coming up where I need to provide a much larger number of one-off codes and I didn't want to have to write hundreds of lines of code with these unique promo codes. I was wondering if there was a way to check if the promo code is correct against the codes in the MySQL database and then if correct redirect the customer to the correct URL? Thanks, any help will be welcome The simple code I have used so far is: <?php $errmsg = ""; if(empty($_POST['pode'])) { $errmsg = 'Enter promotional code'; include 'promoform.php'; } else { switch($_POST['pcode']) { case "code123": $redirectpage = 'http://www.website.com'; break; case "code456": $redirectpage = 'http://www.website.com'; } header("Location: $redirectpage"); } ?> Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 14, 2016 Share Posted January 14, 2016 Sure there is. I'm not sure if you want a different page per code or not, but the following is assuming different pages. First thing would be to make sure your DB is setup right so that it includes Offer Code, Status and URL. Then you have your script SELECT the url that is attached to the offer code SELECT url FROM offerTable WHERE offerCode = :oCode AND status = "active" Then sense-check the results to make sure it's a valid code and a valid response from the DB then just use the returned URL to send people where you want them to go. It also completely removes the need for your CASE. Have a bash and let us know how you get on. Note: the Status allows you to enable and disable offer codes, but if you are doing this you would want to set up some form of persistence. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 14, 2016 Share Posted January 14, 2016 Another option, instead of the "active" flag is to hold "valid_from" and "valid_to" dates, then SELECT url FROM offerTable WHERE offerCode = :oCode AND CURDATE() BETWEEN valid_from AND valid_to Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 14, 2016 Share Posted January 14, 2016 Another option, instead of the "active" flag is to hold "valid_from" and "valid_to" dates, then SELECT url FROM offerTable WHERE offerCode = :oCode AND CURDATE() BETWEEN valid_from AND valid_to I think we've been involved with more dates on here today than I've had with my wife! lol To keep the theme you could also add "issued date" and "redeemed date" fields which could help a lot with sales metrics later on. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.