0

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");
}); 

});

5
  • I'm confused. If this is in node, on the server, why does it need a document ready? And a click event for that matter ... Commented Mar 9, 2018 at 18:21
  • Sorry for the confusion, @Taplar. This code is a mess, I have been trying all the things I know...I know how to do an onclick,etc. Imagine if a three year old was coding. If there is a better/easier way to do it, I'm all ears Commented Mar 9, 2018 at 18:26
  • Well, it's just strange. Some of this is node logic, like the call to the sql, cause you wouldn't put that on the frontend for users to see in their inspector. But then you have your click logic that would be on the client side. So what you seem to be missing here is a link between the front end doing the interaction, and a call to the backend to perform the database logic. Which can be done with a form submit, or ajax. Commented Mar 9, 2018 at 18:28
  • Could you explain further? I'm not sure what you mean by a form submit to link the front and backend Commented Mar 9, 2018 at 22:14
  • So you really don't want to put your sql stuff on the client. Because it has the user name, password, and all the stuff related to the structure of the database to the user. You want that stuff on the backend, on your server. But then in order to perform it, you have to invoke whatever script contains that logic. Which would be to go to that script by either a browser navigating to it, or a form submitting to it, or an ajax request requesting it. Commented Mar 9, 2018 at 22:19

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.