首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我有一个关于将数据从用户传输到另一个用户的问题

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

Stack Overflow用户
提问于 2019-07-21 23:24:43
回答 1查看 35关注 0票数 -1

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

我试过很多方法让它工作,但没有成功。

代码语言:javascript
复制
// 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时,他不能转移任何硬币。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-21 23:56:32

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

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

代码语言:javascript
复制
<?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

希望这能有所帮助!

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57134546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档