Voucher codes claim and admin.


Put this into your phpmyadmin
PHP Code
  1. CREATE TABLE `vouchers` (
  2. `id` int(11) NOT NULL auto_increment,
  3. `voucher` varchar(225) NOT NULL,
  4. `value` varchar(225) NOT NULL,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=MyISAM;


Save as voucher_admin.php
PHP Code
  1. <?php
  2. session_start();
  3. include "config.php";
  4. if($logged[username] && $logged[userlevel] == 6)
  5. {
  6. switch($_GET[page])
  7. {
  8. default:
  9. echo ("
  10. Hello. Would you like to create a voucher?<br>
  11. <form method='post' action='?page=create'>
  12. <b>Value:</b> <input type='text' name='value' maxlength='3'><br>
  13. <input type='submit' value='Claim' name='submit'>
  14. </form>
  15. <table width='200'>
  16. <tr>
  17. <td width='150'><b>Voucher Code</b></td>
  18. <td width='50'><b>Value</b></td>
  19. </tr>");
  20. $fetch = mysql_query("SELECT * FROM `vouchers`");
  21. $rows = mysql_num_rows($fetch);
  22. if ($rows = 0)
  23. {
  24. echo ("<tr>
  25. <td width='150'><b>No Vouchers</b></td>
  26. <td width='50'><b>No Vouchers</b></td>
  27. </tr>");
  28. }
  29. while ($vc = mysql_fetch_array($fetch))
  30. {
  31. echo ("<tr>
  32. <td width='150'>$vc[voucher]</td>
  33. <td width='50'>$vc[value]</td>
  34. </tr>");
  35. }
  36. echo ("</table>");
  37. break;
  38.  
  39. case 'create':
  40. $value = $_POST[value];
  41. $random = "abcdefghijklmnopqrstuvwxyz";
  42. $voucher = substr(str_shuffle($random), 0, 10);
  43. $insert = mysql_query("INSERT INTO `vouchers` (`voucher`, `value`) VALUES ('$voucher', '$value')");
  44. echo ("Congratulations, you have created a voucher worth $vc[value] points. The voucher code is $voucher");
  45. break;
  46. }
  47. }
  48. ?>

Our admin page, lets us create voucher codes.
Random string lets us create one so its not easy to guess.

Save as claim.php
PHP Code
  1. <?php
  2. session_start();
  3. include "config.php";
  4. if($logged[username])
  5. {
  6. switch($_GET[page])
  7. {
  8. default:
  9. echo ("
  10. Hello. Would you like to claim a voucher?<br>
  11. <form method='post' action='?page=claim'>
  12. <b>Voucher Code:</b> <input type='text' name='voucher' maxlength='10'><br>
  13. <input type='submit' value='Claim' name='submit'>
  14. </form>
  15. ");
  16. break;
  17.  
  18. case 'claim':
  19. $voucher = $_POST[voucher];
  20. $fetch = mysql_query("SELECT * FROM `vouchers` WHERE `voucher` = '$voucher'");
  21. $rows = mysql_num_rows($fetch);
  22. if ($rows == 0)
  23. {
  24. echo ("Invalid Reward code!");
  25. }
  26. else
  27. {
  28. $vc = mysql_fetch_array($fetch);
  29. if ($voucher = $vc[voucher])
  30. {
  31. $points = $logged[points]+$vc[value];
  32. $insert = mysql_query("UPDATE `members` SET `points` = '$points' WHERE `username` = '$logged[username]'");
  33. $delete = mysql_query("DELETE FROM `vouchers` WHERE `id` = '$vc[id]'");
  34. echo ("Congratulations, you have claimed a voucher worth $vc[value] points.");
  35. }
  36. }
  37. break;
  38. }
  39. }
  40. ?>

This page you can enter in your voucher code it verfies that it actually is a code and adds the points to the user if it is.

Lightly explained did it in a rush but there shouldn't be any errors.
SkillMaster's Avatar
Views:
3,079
Rating:
Posted on Thursday 17th April 2008 at 03:26 PM
Gork
Gork's Avatar
Nice tut
Posted on Sunday 1st July 2007 at 08:19 AM
SkillMaster
SkillMaster's Avatar
Ya haha I took the header off my old tutorial I couldn't be stuffed re-writing it out.
Posted on Sunday 1st July 2007 at 12:51 AM
-=InSaNe=-
-=InSaNe=-'s Avatar
Im testing, but if im not mistaken 'pet_config.php' should not be there?
Posted on Saturday 30th June 2007 at 09:49 PM
SkillMaster
SkillMaster's Avatar
Comments any errors?