简体   繁体   English

对PHP函数的AJAX调用不会使它为我的数据库输入值

[英]AJAX call on PHP function won't make it enter a value to my Database

I've been working on a piece of code for a few days now but I somehow can't get this to pass the value 1 to my database. 我已经在一段代码上工作了几天,但我不知道怎么把这个值传递给我的数据库。 Basically what I am trying to achieve is that once the user clicks the button "collect coins" it passes the value 1 to my database. 基本上我想要实现的是,一旦用户点击“收集硬币”按钮,它就将值1传递给我的数据库。 Every day at 12 pm the column "dailyfree" is reset to 0 by a MySQL event so the next day the user can click the button again since the value of "dailyfree" in the database is 0 again. 每天中午12点,“dailyfree”列被MySQL事件重置为0,因此第二天用户可以再次单击该按钮,因为数据库中“dailyfree”的值再次为0。 Still I don't want the user to be able to click "collect coins" before the value in the database has been reset. 我仍然不希望用户能够在重置数据库中的值之前单击“收集硬币”。 The problem is that once the user click the button, it gives him the free coins but the value 1 does not get passed to the database. 问题是,一旦用户点击按钮,它就会给他免费的硬币,但值1不会传递给数据库。 The column "dailyfree" is still 0 after clicking the button. 单击按钮后,“dailyfree”列仍为0。 Does anybody know why that is? 有谁知道那是为什么?

Just for your information: I am very new to coding! 仅供参考:我对编码很新!

Here is what I am working with right now. 这就是我现在正在使用的内容。

Javascript: 使用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)
}

The 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 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 won't enter the value into my database! 我真的希望你能帮助我找出为什么它不会将值输入我的数据库! Anyways, I wish you a very nice day :) Thanks for your help and time! 无论如何,祝你有个美好的一天:)谢谢你的帮助和时间!

The query is wrong. 查询错误。

Should be a update query. 应该是更新查询。

$db->exec('UPDATE users SET dailyfree = '.$dailyfree.' , balance = balance + '.$freecoins.' WHERE userid = '.$db->quote($user['userid'])); $db->exec('UPDATE users SET dailyfree = '.$dailyfree.' , balance = balance + '.$freecoins.' WHERE userid = '.$db->quote($user['userid']));

Or if you want to update when the user record present else insert the data then take a look at ON DUPLICATE KEY 或者,如果要在用户记录存在时更新,则插入数据,然后查看ON DUPLICATE KEY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM