Advertisement
gitlez

YA: Simply Adding and Removing Points From Users WOC

Apr 13th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.76 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. /*    Place your Database Connection Stuff Here, or Include the connection file    */
  5.  
  6. /*    Functions    */
  7. function getPts($username){
  8.     $result = mysql_query("SELECT pts FROM users WHERE user_name='{$username}' LIMIT 1") or die('MySQL Query Error: ' . mysql_error());
  9.     return mysql_fetch_object( $result )->pts;
  10. }
  11. function updatePts($username, $pts){
  12.     $result = mysql_query("UPDATE users SET pts=pts+({$pts}) WHERE user_name='{$username}' LIMIT 1") or die('MySQL Query Error: ' . mysql_error());
  13.     return $result;
  14. }
  15. if( $_SERVER['REQUEST_METHOD'] === 'GET' ){
  16.     $username = mysql_real_escape_string( $_SESSION['username'] );
  17.     $pointsAvail = $_SESSION['pts'] = getPts($username);
  18.     if($pointsAvail > 0){
  19.         echo '
  20.            <form method="post">
  21.                ' . $username . ', you currently have: <span style="font-weight: bold;">' . $pointsAvail . '</span> pts Available to transfer.<br>
  22.                Send: <input type="text" name="formValue" value="' . $pointsAvail . '"> pts<br>
  23.                To: <input type="text" name="recipient"><br>
  24.                <input type="submit" value="Send Pts">
  25.            </form>
  26.        ';
  27.     }else{
  28.         echo 'Sorry, ' . $username . ', you do not have any points to transfer.';
  29.     }
  30. }else{
  31.  
  32.     /*    Form Has Been Submitted, Update Necessary Fields    */
  33.     $username = mysql_real_escape_string( $_SESSION['username'] );
  34.     $recipient = mysql_real_escape_string( $_POST['recipient'] );
  35.     $formValue = abs( $_POST['formValue'] );
  36.     $errorMsg = '';
  37.  
  38.     /*    Error Checking    */
  39.     if(strlen($recipient) === 0){
  40.         $errorMsg .= 'You need to enter a recipient.<br>';
  41.     }
  42.     if($formValue > $_SESSION['pts']){
  43.         $errorMsg .= 'You do not have enough points (' . $_SESSION['pts'] . ' pts) to transfer ' . $formValue . ' pts.<br>';
  44.     }
  45.     if($recipient === $username){
  46.         $errorMsg .= 'There\'s no point in sending points to yourself.<br>';
  47.     }else if(!@mysql_query("SELECT pts FROM users WHERE user_name='{$recipient}' LIMIT 1")){
  48.         $errorMsg .= 'The Recipient selected does not appear to exist.<br>';
  49.     }
  50.  
  51.     /*    If Errors, display Message and end    */
  52.     if( strlen($errorMsg) > 0){
  53.         echo 'The following errors occurred: <br>' . $errorMsg;
  54.     }else if( updatePts( $recipient, $formValue ) && updatePts($username, ($formValue * (-1))) ) {
  55.         $_SESSION['pts'] -= $formValue;
  56.         echo '<h3>Success</h3><p>You gave sent ' . $formValue .' pts to ' . $recipient . '.</p>' .
  57.                 '<p>You currently have: ' . $_SESSION['pts'] . ' pts remaining.</p>';
  58.     }else{
  59.         echo '<h3>Error</h3><p>An internal server error has stopped this process. Please try again in a few minutes.</p>';
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement