Advertisement
gitlez

YA: Simply Adding and Removing Points From Users WC

Apr 13th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.80 KB | None | 0 0
  1. <?php
  2. // In Response to Yahoo Answer's Question
  3. /*    
  4.     Going to use sessions to save some system resources, by only querying when needed.
  5.     http://ca.php.net/manual/en/features.sessions.php
  6. */
  7. session_start();
  8.  
  9. /*    Place your Database Connection Stuff Here, or Include the connection file    */
  10.  
  11. /*    Functions    */
  12. function getPts($username){
  13.     $result = mysql_query("SELECT pts FROM users WHERE user_name='{$username}' LIMIT 1") or die('MySQL Query Error: ' . mysql_error());
  14.     return mysql_fetch_object( $result )->pts;
  15. }
  16. function updatePts($username, $pts){
  17.     $result = mysql_query("UPDATE users SET pts=pts+({$pts}) WHERE user_name='{$username}' LIMIT 1") or die('MySQL Query Error: ' . mysql_error());
  18.     return $result;
  19. }
  20.  
  21. if( $_SERVER['REQUEST_METHOD'] === 'GET' ){
  22.     $username = mysql_real_escape_string( $_SESSION['username'] ); // I don't know your method of keeping the user's username. ** DO NOT USE PLAIN COOKIES **
  23.     $pointsAvail = $_SESSION['pts'] = getPts($username);
  24.     /*
  25.         Seeing as the User is the one with the ability to send the pts, there is no need for them to be included in the form, all that does is allow someone to change it. Then you have complete kaos.
  26.     */
  27.     if($pointsAvail > 0){
  28.         echo '
  29.            <form method="post">
  30.                ' . $username . ', you currently have: <span style="font-weight: bold;">' . $pointsAvail . '</span> pts Available to transfer.<br>
  31.                Send: <input type="text" name="formValue" value="' . $pointsAvail . '"> pts<br>
  32.                To: <input type="text" name="recipient"><br>
  33.                <input type="submit" value="Send Pts">
  34.            </form>
  35.        ';
  36.     }else{
  37.         echo 'Sorry, ' . $username . ', you do not have any points to transfer.';
  38.     }
  39. }else{
  40.     /*    Form Has Been Submitted, Update Necessary Fields    */
  41.     $username = mysql_real_escape_string( $_SESSION['username'] );
  42.     $recipient = mysql_real_escape_string( $_POST['recipient'] );
  43.     $formValue = abs( $_POST['formValue'] );
  44.     /*
  45.         I used abs(), because it will automatically convert negative numbers to possitive (It gets the absolute value of the number).
  46.         Imagine if you simply subtracted the values, and someone entered (-10). You SQL statement would then be:
  47.         ... SET pts=pts-(-10) ... Which would actually add points.
  48.     */
  49.     $errorMsg = ''; // Will hold the error Message, if any
  50.    
  51.     /*    Error Checking    */
  52.     if(strlen($recipient) === 0){
  53.         $errorMsg .= 'You need to enter a recipient.<br>';
  54.     }
  55.     if($formValue > $_SESSION['pts']){
  56.         $errorMsg .= 'You do not have enough points (' . $_SESSION['pts'] . ' pts) to transfer ' . $formValue . ' pts.<br>';
  57.     }
  58.     if($recipient === $username){
  59.         $errorMsg .= 'There\'s no point in sending points to yourself.<br>';
  60.     }else if(!@mysql_query("SELECT pts FROM users WHERE user_name='{$recipient}' LIMIT 1")){ // The column pts is not important, but it was just a simple column that I knew existed, that's all
  61.         $errorMsg .= 'The Recipient selected does not appear to exist.<br>';
  62.     }
  63.    
  64.     /*    If Errors, display Message and end    */
  65.     if( strlen($errorMsg) > 0){
  66.         echo 'The following errors occurred: <br>' . $errorMsg;
  67.     }else if( updatePts( $recipient, $formValue ) && updatePts($username, ($formValue * (-1))) ) { // Continue with the processing of the form. Value * -1 to convert it to a negative number
  68.         $_SESSION['pts'] -= $formValue;
  69.         echo '<h3>Success</h3><p>You gave sent ' . $formValue .' pts to ' . $recipient . '.</p>' .
  70.                 '<p>You currently have: ' . $_SESSION['pts'] . ' pts remaining.</p>';
  71.     }else{
  72.         echo '<h3>Error</h3><p>An internal server error has stopped this process. Please try again in a few minutes.</p>';
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement