'Updating MySQL database with jquery, displaying update on page

Standard disclaimer--I am brand new to coding and I have never used a MySQL database before. I also do not know PHP (I know that many of the answers on this site include using it). I do know how to use node.js (as much as I know how to use any of this...)

What I'd like to do: Have a simple "like" feature that displays how many users have liked an event. I know how to write an onclick function, so right know the likes are increasing--but I need to link it to my database so that number of likes pulls through from one session to the next.

This is my database schema:

CREATE TABLE `likes` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`likeCounter` INT(11) NULL DEFAULT '0',
PRIMARY KEY (`id`))

This is my jQuery code:

$(document).ready(function() {
// counter variable
var counter = 0;
// connect to mysql
var mysql = require("mysql");
// create the connection information for the sql database
var connection = mysql.createConnection({
  host: "localhost",
  port: 3306,
  // Your username
  user: "root",
  // Your password
  password: "root",
  database: "quickiehelp"
});
// connect to the mysql server and sql database
connection.connect(function(err) {
  if (err) throw err;
  // run the start function after the connection is made to prompt the user
  start();
  // counter function
function heartClick() {
  counter++;
  $('.clicks').html(counter);
  $"counter" = mysql_query("UPDATE `likes` SET `likeCounter");
}

$('#heartContainer').click(function(){ 
  console.log("alert");
  heartClick();
  $('#heartContainer').toggleClass("animated pulse");
}); 

});



Solution 1:[1]

Your code explaining that you are simply using some client side code in your HTML or PHP or any such file and you are using the jquery to manage client-side events.

You need to use ajax (available in jquery) to hit some server side script that should interact with database server instead of having this DB connection and DB related logic on the client side.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adal Singh