Logic of a Likes table

Asked

Viewed 80 times

0

In a PHP + Mysql application in which I need to store how many Likes + id’s of who gave like in a publication, I came up with a question about the logic.

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

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

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

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

My fear is that while PHP treats the current Ids to concatenate into the database string, someone else gives a like and captures the values of the database without the ID being handled and overwrites it when it completes.

Is there a smarter way to inhibit this problem? Maybe run something directly on Mysql.

  • 2

    His concern is consistent, as there is clear competition in this case. However, the structure of its table, which I do not think is the ideal one, is what provides such a situation. Research relationships between tables and see how to implement the "many to many" relationship structure (in English, Many-to-Many).

1 answer

2


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

Create a table that contains the id of the postage and the id user’s:

When any user likes a post, you do the insert, when he neglects you make a delete.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.