Logic of a Likes table

0

In a PHP + MySQL application in which I need to store as many likes + id's of who I gave in a post, I came to doubt the logic.

Assuming 2 users give like in the same post at the same time, and both execute:

UPDATE te_anuncios
SET curtidasTotal = curtidasTotal + 1,
    curtidasIds   = curtidasIds + idNovo; //iria geral algo como '23, 31, 43, 44, 32...'

I need to handle this update in PHP, to add the ID of the new tanner, right?

<?php
  $idsAtuais .= ", {$_SESSION['userId']}";

My fear is that while PHP handles the current IDs to concatenate the database string, someone else takes a liking and captures the values of the database without the ID being treated and overrides it when it completes.

Would there be a smarter way that would inhibit this problem? Maybe run something directly in MySQL.

    
asked by anonymous 03.02.2018 / 12:49

1 answer

2

This is a matter of normalizing your database, whenever you have the relation of N:N it is necessary to create an auxiliary table.

Create a table that contains the id of the post and the id of the user:

When a user enjoys a post, you make insert , when he writes you make a delete .

    
03.02.2018 / 12:56