Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Help: Creating Coupon login

login

  • Please log in to reply
18 replies to this topic

#1 wheay

wheay

    CC Regular

  • Member
  • PipPipPip
  • 33 posts

Posted 23 February 2012 - 11:10 PM

Hi,

Can you help me, i need to create a coupon code login with my database.

i have a database of coupon codes, the form needs to enter the code, if the code is entered correctly it will go to another page. if the code is taken it will delete it from the database automatically so no one can use it again. im using php with this one.

please make the code simple as possible.

thank you.
  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 24 February 2012 - 08:06 AM

it's pretty simple, so I won't do it for you. but something like

check if cupon is in table (with an query to your database)
make login procedure (for example setting a session variable and similar)
delete cupon (with a new database query)
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 Jmancuso

Jmancuso

    CC Regular

  • Member
  • PipPipPip
  • 45 posts

Posted 24 February 2012 - 07:07 PM

its extemely late so dont mind me if the code is a little off but:
the query can look something like this :


echo "<form action='page.php' method='POST'>
Enter Coupon Code: <input type = 'text' name='coupon_number' >
</form>";

//rename post variable (makes it easier)
$coupon_number = $_POST['coupon_number'];

//check if it exists
$query_check = mysql_query("SELECT * FROM table_name WHERE column_name = $coupon_number");

//see if it returns true and if it does, delete it, if not then... idk. do whatever u want (say that code does not exist?)
if ($query_check){
$query_delete = mysql_query("DELETE FROM table_name WHERE coupon_number = $coupon_number");
}else {echo 'code does not exist you lying sack of **** stop trying to rip me off.';}


you get the basic idea though. is this what your asking about?
  • 0

#4 wheay

wheay

    CC Regular

  • Member
  • PipPipPip
  • 33 posts

Posted 25 February 2012 - 05:00 AM

its extemely late so dont mind me if the code is a little off but:
the query can look something like this :


echo "<form action='page.php' method='POST'>
Enter Coupon Code: <input type = 'text' name='coupon_number' >
</form>";

//rename post variable (makes it easier)
$coupon_number = $_POST['coupon_number'];

//check if it exists
$query_check = mysql_query("SELECT * FROM table_name WHERE column_name = $coupon_number");

//see if it returns true and if it does, delete it, if not then... idk. do whatever u want (say that code does not exist?)
if ($query_check){
$query_delete = mysql_query("DELETE FROM table_name WHERE coupon_number = $coupon_number");
}else {echo 'code does not exist you lying sack of **** stop trying to rip me off.';}


you get the basic idea though. is this what your asking about?



i think i get the logic now. thank for your help. i just wonder how about if the user fails to submit the information after entering the coupon code i mean he accidentally hit back. so the coupon code he have is now deleted. hummm.

the coupon code is like a password with multiple user. something like that.
  • 0

#5 Jmancuso

Jmancuso

    CC Regular

  • Member
  • PipPipPip
  • 45 posts

Posted 25 February 2012 - 08:00 AM

the code will not be deleted until he hits the submit button. So if he enters the data into the form, and backspaces, nothing will happen. If he hits submit, gets redirected to the page, and then hits backspace, that could be a problem, best bet is to make a cookie (if you want the code to last a certain time for the user) or a session (if you want the code to last until the user closes the browser). Orr the second best bet will be to redirect him to a redirect page, which sets cookies (maybe even records his ip in a database?) And then direct him to the page. So if he backspaces, ** just be redirected back to the same page like a continous loop, if you want it perfect, it will get kinda advanced but ill tell you how in a few hours

Edited by Jmancuso, 25 February 2012 - 10:40 AM.

  • 0

#6 Jmancuso

Jmancuso

    CC Regular

  • Member
  • PipPipPip
  • 45 posts

Posted 25 February 2012 - 07:49 PM

this can be done by a cookie. you can make the coupon code last the user a certain amount of time (lets say 10 minutes for ease of math) so lets say they enter the coupon code and get redirected to the member.php page. on this login page, lets make a session. (session and cookies are essentially the same, though they have a small amount of differences, the differences are kind of big, you can set a session anywhere, but you can only set a cookie before any output is displayed. kind of like a header. or so ive read.) now lets make a session:

this is going in the login page


//CHANGE from last point is here
session_start();
//in order to start any sessions, you have to call this function.

echo "<form action='page.php' method='POST'>
Enter Coupon Code: <input type = 'text' name='coupon_number' >
</form>";

//rename post variable (makes it easier)
$coupon_number = $_POST['coupon_number'];

//check if it exists
$query_check = mysql_query("SELECT * FROM table_name WHERE column_name = $coupon_number");

//see if it returns true and if it does, delete it, if not then... idk. do whatever u want (say that code does not exist?)
if ($query_check){
$query_delete = mysql_query("DELETE FROM table_name WHERE coupon_number = $coupon_number");

//this is the difference right here from my last reply.
$sess_logged = $_SESSION['logged'] = "logged";
//now on your members page, change this session into a cookie that will last 10 minutes. we will do that now in my next code tags.

}else {echo 'code does not exist you lying sack of **** stop trying to rip me off.';}



time for conversion:



//on your members page.
session_start();
$sess_logged = $_SESSION['logged'];

//now a cookie looks like this: setcookie('name', 'value', expration_date); expiration date is in seconds.
//60 seconds in a minute. 60 * 10 = 600 seconds OR 10 minutes

if (isset($sess_logged)){
setcookie('logged_in', 'logged', time()+600);
}
//now theres a cookie set, if u log out, click backspace, close the browser, restart ur computer, ur cookie will be good for 10 minutes. although if you do //everything but backspace, your session will be lost, but your cookie will still be good.



does this help a little bit more? if you want multiple coupons codes: have a num_left column in your table. so you can set this query



$coupon_code = $_POST['coupon_code'];

//if the user entered a correct coupon, run this:
$query = "UPDATE table_name SET num_left = num_left - 1 WHERE coupon_code = $coupon_code";
//this code minus's 1 everytime the code is ran. once its down to 0, it will still remain. i dont think you want that. so lets do:

$query_delete = "SELECT num_left FROM table_name WHERE coupon_code = $coupon_code";
if ($query_delete == 0){
mysql_query("DELETE FROM table_name WHERE num_left = 0 AND coupon_code = $coupon_code");
}



hope this helps, leave some feedback on your thoughts please and let me know if you understand or dont understand

Edited by Jmancuso, 26 February 2012 - 06:59 AM.

  • 0

#7 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 25 February 2012 - 08:14 PM

[COLOR=#000000][COLOR=#0000BB]$query [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#DD0000]"UPDATE table_name SET num_left = num_left - 1 WHERE coupon_code = $coupon_code"[/COLOR][COLOR=#007700]; 

This isn't a good idea. For auditing purposes you shouldn't do this but rather have a table CouponsRedeemed. By this I mean you want the ability to know how many coupons total there are and have the ability to know which coupons were used and by whom and when it was used. So don't do the num_left = num_left - 1

Keep that field named NumCoupons or something and have a table CouponsRedeemed which stores the CouponCode, DateRedeemed and some way of knowing who used that coupon perhaps.


[/COLOR][/COLOR]
  • 0

#8 Jmancuso

Jmancuso

    CC Regular

  • Member
  • PipPipPip
  • 45 posts

Posted 26 February 2012 - 07:00 AM

hm. good idea
  • 0

#9 wheay

wheay

    CC Regular

  • Member
  • PipPipPip
  • 33 posts

Posted 29 February 2012 - 06:43 PM

Thank you all for your help. this is exactly what i need.
  • 0

#10 wheay

wheay

    CC Regular

  • Member
  • PipPipPip
  • 33 posts

Posted 01 March 2012 - 06:16 PM

Hi im now having a problem between my Javascript verification and my php verification. can you help me?

how do i call the php verification first before the javascript. i think its placing php over the top of js?
  • 0

#11 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 01 March 2012 - 06:19 PM

Well the PHP runs before the page appears in the browser so yes the PHP will always run before any JS code. I'm not sure what you want help with? :o
  • 0

#12 wheay

wheay

    CC Regular

  • Member
  • PipPipPip
  • 33 posts

Posted 04 March 2012 - 05:21 PM

Well the PHP runs before the page appears in the browser so yes the PHP will always run before any JS code. I'm not sure what you want help with? :o


the problem im having right now is that i have the validation if the coupon code exists in the db using php, and i also have the validation for the form using js. now the problem, no matter what place i put the php validation, it still sends the information but sends me also that the coupon code is not valid. how do i connect the two. if you want the code i can send it to you. thank you.
  • 0





Also tagged with one or more of these keywords: login

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download