S
S
Sergey Defactov2017-10-10 22:48:51
PHP
Sergey Defactov, 2017-10-10 22:48:51

How to display an error if the balance is less than the amount?

Good evening, please help me figure out the function, when you click on Add to BP (post), it goes to the admin panel for moderation, while the user is deducted -10 points from the balance, how to do it so that if the user has a balance = 0 or a balance less than the amount, the function does not worked and a message was displayed
now everything is working the balance is removed but if the user has a balance < the amount, then the post is still sent for moderation, that is, the succes
function is triggered

function add_deport($val) {
    $expected = array(
        'link' => '',
        'type' => '',
        'reason' => ''
    );

    /**
     * @var $link
     * @var $type
     * @var $reason
     */
    extract(array_merge($expected, $val));
    $time = time();
        $db = db();
         $user_id = get_userid();
    $link = sanitizeText($link);
    $type = sanitizeText($type);
    $reason = sanitizeText($reason);
$credit=get_creditgift_balance($user_id); // отображает баланс пользователя
       $amount=10; // сума транзакции
       $balance=$credit[0]['credit_balance'];
            $new_balance=$balance-$amount;  
            $query=$db->query("UPDATE users SET credit_balance='$new_balance' WHERE id='$user_id'");
    if(!has_deported($link, $user_id)){
        db()->query("INSERT INTO deports (user_id, type, link, message, time) VALUES('".$user_id."', '".$type."', '".$link."', '".$reason."', '".$time."')");
        fire_hook('deport.added', null, array($val));
        return true;
    }  elseif($amount < $balance OR $amount != $balance){
        return false;
    }
}

JS
$(document).on('submit', '#deportModal form', function() {
        $(this).css('opacity', '0.6');
        var sM = $("#deportModal").data('success');
        var eM = $("#deportModal").data('error');
        $(this).ajaxSubmit({
            url : baseUrl + 'deport',
            success : function () {
                $("#deportModal").modal('hide');
                notifySuccess(sM);
                $(this).css('opacity', 1);
            },
            error : function () {
                $("#deportModal").modal('hide');
                notifyError(eM);
            }
        })
        return false;
    });
})

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel Gogolinsky, 2017-10-10
@cashloveyou

In php code, after you know the balance, compare it with the amount of the transaction, and if the balance is not enough, discard the exception.

R
rorc, 2017-10-10
@rorc

Before you do

UPDATE users SET credit_balance='$new_balance' WHERE id='$user_id'"

Check your current balance first
if it is less than 10, then redirect to the balance replenishment page.

M
Michael, 2017-10-10
@springimport

I hope that you will come to the bright side of clean code.
And the solution without exceptions will look something like this.

function add_deport($val) {
    $expected = array(
        'link' => '',
        'type' => '',
        'reason' => ''
    );

    extract(array_merge($expected, $val));
    $time = time();
    $db = db();
    $user_id = get_userid();
    $link = sanitizeText($link);
    $type = sanitizeText($type);
    $reason = sanitizeText($reason);
    $credit = get_creditgift_balance($user_id); // отображает баланс пользователя
    $balance = $credit[0]['credit_balance'];
    $amount = 10; // сума транзакции

    if ($amount < $balance) {
        $new_balance = $balance - $amount;
        $db->query("UPDATE users SET credit_balance='$new_balance' WHERE id='$user_id'");
        $db->query("INSERT INTO deports (user_id, type, link, message, time) VALUES('" . $user_id . "', '" . $type . "', '" . $link . "', '" . $reason . "', '" . $time . "')");
        fire_hook('deport.added', null, array($val));

        if (has_deported($link, $user_id)) {
            return true;
        }
    }
    
    return false;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question