I have been working on a piece of code for several days, but for some reason I cannot get this to pass the value 1 to my database. Basically, I try to ensure that as soon as the user clicks the collect coins button, he passes the value 1 to my database. Every day at 12 o’clock, the "dailyfree" column is reset to 0 by the MySQL event, so the next day the user can click the button again, because the "dailyfree" value in the database is 0. However, I do not want the user to click "collect coins" before the value in the database was reset. The problem is that as soon as the user clicks the button, he gives him free coins, but the value 1 is not transferred to the database. After clicking the button, the "dailyfree" column remains 0. Does anyone know why this is?
For your information only: I am very new to coding!
This is what I am working on right now.
JavaScript:
function free(){ var str = "username"; var term = "searched_word"; var index = str.indexOf(term); if (index != -1) { var daily = 1; $.ajax({ url:"/free?daily="+daily, success:function(data){ try{ data = JSON.parse(data); console.log(data); if(data.success){ bootbox.alert("Success! You've claimed 100 free credits!"); }else{ bootbox.alert(data.error); } }catch(err){ bootbox.alert("Javascript error: "+err); } }, error:function(err){ bootbox.alert("AJAX error: "+err); } }); } else { bootbox.alert("Your username does not contain the searched_word!");} } function disDelay(obj){ obj.setAttribute('disabled','disabled'); setTimeout(function(){obj.removeAttribute('disabled')},86400) }
Button itself
<div class="text-right"> <button class="btn btn-success btn-block" id="free" onclick="free(); disDelay(this)">Collect free coins</button> </div>
Php
$freecoins = 100; case 'free': if(!$user) exit(json_encode(array('success'=>false, 'error'=>'You must login to access the redeem.'))); $daily = 1; if(!preg_match('/^[a-zA-Z0-9]+$/', $daily)) { exit(json_encode(array('success'=>false, 'error'=>'Code is not valid'))); } else { $sql = $db->query('SELECT * FROM `users` WHERE `dailyfree` = '.$db->quote($dailyfree)); if($sql->rowCount() != 0) { $row = $sql->fetch(); if($row['user'] == $user['userid']) exit(json_encode(array('success'=>false, 'error'=>'You have already redeemed your daily reward!'))); $db->exec('INSERT INTO `users` SET `dailyfree` = '.$dailyfree.' , `balance` = `balance` + '.$freecoins.' WHERE `userid` = '.$db->quote($user['userid'])); exit(json_encode(array('success'=>true, 'credits'=>$freecoins))); } else { exit(json_encode(array('success'=>false, 'error'=>'Code not found'))); } } break;
I really hope you can help me find out why it will not make a difference to my database! In any case, I wish you a very pleasant day :) Thanks for your help and time!