简体   繁体   English

单击按钮,MySQL更新值+1(增量)

[英]MySQL update value +1 (increment) on button click

I know this question has been answered over a million times, but I'm very new to the whole programming scene and I just can't get it to work. 我知道这个问题已经被回答了一百万遍了,但是对于整个编程领域我还是很陌生,我无法使它正常工作。 Sorry, I hope you can help me! 对不起,希望您能帮帮我!

HTML: HTML:

<div class="the_score">+76</div>
<div id="increment_value">Like</div>

I have a MySQL connection and I can fetch the data, but I want the score to update with +1 when someone clicks the Like div. 我有一个MySQL连接,可以获取数据,但是当某人单击Like div时,我希望分数以+1更新。 I have a table "websites" with row "WebScore" that has the function int(10) (?? I hope that is correct). 我有一个表“ websites”,其中行“ WebScore”具有函数int(10)(??我希望这是正确的)。

I use this to fetch the data and show it on my website: 我用它来获取数据并将其显示在我的网站上:

$r = rand(2,3);
$sql = "SELECT * FROM websites WHERE ID = $r";
$result = $conn->query($sql);
$row = $result->fetch_assoc();

I presume you have a column , not a row , called WebScore. 我假设您有一 ,而不是一行 ,称为WebScore。

You need the query 您需要查询

 UPDATE websites SET WebScore = WebScore + 1 WHERE ID = $r

As you can see, it finds the right row of your table and updates the value of the WebScore column. 如您所见,它找到表的右行并更新WebScore列的值。

This sort of things can only be done from a PHP program. 这类事情只能通过PHP程序完成。 It can't be done directly from a Javascript method invoked within a web browser. 无法直接通过网络浏览器中调用的Javascript方法来完成。 So, if you want to react to a user click, you'll need to post a form or invoke an Ajax style call to a php endpoint. 因此,如果要对用户单击做出反应,则需要发布表单或对php端点调用Ajax样式调用。 How to do that is the province of another question. 如何做到这一点是另一个问题。

Keep plugging away; 保持堵塞; you'll figure out this database stuff. 您将弄清楚这个数据库的东西。

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

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