I think it can be done with php and mysql but I’m not sure, so I ask you.
I have a site where every registered user has coins which are saved on the database.
I have a page with a button, and each user can click this button by spending 1 coin, and by doing this there will be a counter that will count how many coins have been spent (we can also think of them as clicks) but if a user has no coins he is redirected to “./ricarica.php”
any ideas?
Advertisement
Answer
I try to give you a minimal example (from our comment):
DB Structure:
Coin table:
JavaScript
x
ID-NOMECOIN-NUMEROTOTALE
User Table:
JavaScript
IDUSER-NOMEUSER-COINRIMASTI
Html:
JavaScript
<button id="checkcoin" value="<?php echo $row['id']?>">Controllo Coin</button>
//i suppose you have a query for select user id.
Js Call Ajax:
JavaScript
$(function () {
$('#checkcoin').on('click', function () {
var users = document.getElementById('checkcoin').value;
$.ajax({
url: "upd.php",
type: "POST",
data: {users : users},
success: function(data){
//quello che vuoi che succeda.
}
});
});
PHP:
JavaScript
$iduser=$_POST['users'];
$query=$mysqli->prepare('SELECT * FROM users WHERE iduser=?');
$query->bind_param('i',$iduser);
$query->execute();
$store=$query->get_result();
while($res=$store->fetch_array()){
$coinrimasti=$res['coinrimasti'];
}
$nuovocoin=$coinrimasti - 1;
if($nuovocoin=='20'){
FunzioneResetta($iduser); // crei una funzione che resetta entrambi i contatori sia l'user che il totale
echo 'hai vinto un premio';
}elseif($nuovocoin =='0' or $nuovocoin=='-1'){
header(location:/ricarica.php);
}else{
//fai l'update di entrambi i contatori uno in meno uno in più
}