简体   繁体   中英

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:

<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. I have a table "websites" with row "WebScore" that has the function int(10) (?? I hope that is correct).

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.

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.

This sort of things can only be done from a PHP program. It can't be done directly from a Javascript method invoked within a web browser. 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. How to do that is the province of another question.

Keep plugging away; you'll figure out this database stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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