Custom gift function


I’m trying to build a custom function on my website i’m trying to build a custom gift function i want users to buy coins and add them to a database but i’m have trouble selecting if the coin amount is in the database i don’t know how to select like this so lets say i have 200 coins in the database but i want to send i gift that is 100 coins how do i select 100 from 200 amount in the database or i’m i doing it wrong any help.

[php]$stmt = $isv_db->prepare(“SELECT coin_total - $this->coin_total = $this->coin_total FROM users_coins WHERE $this->coin_total=? AND user_id=?”);
$stmt->bind_param(‘i’, $user);
if ($stmt->num_rows() < 1){
$array[‘err’] = true;
$array[‘message’] = ‘You need more coins’;
echo json_encode($array);


You don’t select the specific amount.

You select the amount they have. Then, check to see if they have enough of a balance to transfer to someone else. Next, transfer the amount to the other person. Lastly, deduct the amount from the original person.

Also, this should be done using a sql transaction. Reason being, if either the transfer or the deduction fails, you want to roll back everything.