我有一个关于将数据从用户传输到另一个用户的问题



现在,我有一个小问题。我需要输入才能将硬币从用户发送到另一个用户,那么如何从数据库中获取发送者ID和接收者ID,并且转移完成后,硬币应从发送者中扣除。

我已经尝试了很多方法让它工作,但没有。

// my input
<div class="form-group">
          <label for="coins_send" class="control-label"></label> 
          <input id="coins_send"  type="number" class="form-control">
          <input id="receiver_id" type="hidden" class="form-control">
      </div>
<button onclick="SaveCoinsFor()" class="btn btn-primary"></button>
//  Javascript
SaveCoinsFor= function(){
var new_name =  $('#coins_send').val(),target_id=$('#receiver_id').val();
$.post('system/coins_set.php', { 
    token: utk,
    target_id:target_id ,
    coins_send:coins_send,
    action:"send_coins",
    }, function(response) {
        if(response == 1)callSaved(system.saved, 1);
        else callSaved(system.error, 3);
});
}

我的数据库列user_idcoins所以现在我想设置coins_set.php文件来进行此传输。

注意:我想确保当用户硬币= 0时,他不能转移任何硬币。

我肯定知道我的代码有问题。无论如何,我还在学习,所以也许有人可以帮助我。

这相当简单,因为您已经有一个登录系统。

以下是一些伪代码,可帮助您入门:

<?php
// system/coins_set.php
session_start();
$senderId = $_SESSION['user_id']; // <- depending on how you store the user ofcourse
$targetId = $_POST['target_id']; 
$coinsToSend = $_POST['coins_send']; // We will send 3 coins

// Here you would normally get the balance from the database.
// This will be the total amount of coins the current user has now.
$balance = 4;

// Here we will simply substract the coins we want to send from the total
$newBalance = $balance - $coinsToSend;
// Check if we have enough coins to do this
if ($newBalance < 0) {
    exit('not enough coins');
}
// Update the current users balance
// This will be '1' in our case
// You would do something like this:
// UPDATE users SET coins = $newBalance WHERE user_id = $senderId
// and also update the balance of the target
// Get current coins from targetUser
// SELECT coins FROM users WHERE user_id = $targetId
// let's say the coins is 4
$targetCoins = 4;
$targetCoins += $coinsToSend;
// UPDATE users SET coins = $targetCoins WHERE user_id = $targetId

希望这有帮助!

最新更新