简体   繁体   English

点击+1可增加MySQL值

[英]Increment MySQL value on click with +1

What I have 我有的

An url like this: http://localhost:8888/website/?key=ABC 像这样的网址: http://localhost:8888/website/?key=ABC

A MySQL table with many rows, one with a key called ABC . 一个具有许多行的MySQL表,其中一个键称为ABC I have a button that I want users to click (upvote) the MySQL row corresponding to key=ABC . 我有一个按钮,希望用户单击(更新)与key=ABC对应的MySQL行。

<a href="" onclick="increment()"></a>

In my scripts.js I have this (incomplete?) Ajax code: 在我的scripts.js中,我有以下(不完整的?)Ajax代码:

function increment() {
    $.ajax({
        type: 'POST',
        url: '../js/ajax.php',
        success: function(data) {
            alert("function saved: " + data);
        }
    });
}

And my ajax.php looks like this: 我的ajax.php看起来像这样:

<?php
    if (isset($_GET['key']) {
        $rowKEYtest = $_GET['key'];
        $sql2 = "UPDATE database SET Score = Score + 1 WHERE UniqueKey = '$rowKEYtest'";
        $conn->query($sql2);
    } else {
        echo "lol";
    }
?>

It's not updating. 它没有更新。 I have no idea what to do. 我不知道该怎么做。

Please have a look in your code, There is 2 mistakes. 请查看您的代码,有2个错误。

1. In your ajax call you are using  type: 'POST', you should use GET.
2. You are nor parsing your 'key' param in ajax call.

Actually you are using POST method in ajax but in ajax.php trying to get the value with GET method. 实际上,您在ajax中使用POST方法,但在ajax.php中尝试使用GET方法获取值。 second thing you are not passing any param in ajax call. 第二件事,您没有在ajax调用中传递任何参数。

Hope this will help. 希望这会有所帮助。

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

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