Reputation: 45
I want the code to work so that when users earn an amount it should be added to the Paid
table, e.g. after adding it should contain User amount + Earned
My Code currently adds a new row on every claim, but I want instead to update the users' balance if an address
is already in the table.
Example: User Balance is 15.2 and they earned 2.3 this balance should be updated to 17.5
$address = trim($_POST["address"]);
$q = $sql->prepare("INSERT INTO ".$dbtable_prefix."Paid
SET
`address` = ?,
`amount` = ?
;");
$claimlog_reward = $reward;
if ($reward<1) {
$claimlog_reward = number_format($reward, 8, '.', '');
}
$
q->execute(array(trim($_POST['address']), $claimlog_reward));
Upvotes: 0
Views: 47
Reputation: 784
I'll assume MySQL / MariaDB - Add a ON DUPLICATE KEY
update (provided your address is a primary key)
$address = trim($_POST["address"]);
$claimlog_reward = $reward;
if ($reward<1) {
$claimlog_reward = number_format($reward, 8, '.', '');
}
$q = $sql->prepare("INSERT INTO ".$dbtable_prefix."Paid
SET `address` = :address, `amount` = :amount
ON DUPLICATE KEY UPDATE amount = amount + :amount");
$q->bindValue(':address', trim($_POST['address']));
$q->bindValue(':amount', $claimlog_reward);
$q->execute();
Upvotes: 1