简体   繁体   English

php-mysql重复行

[英]Php-mysql duplicated rows

I made a like-unlike feature in my iOS-PHP Application. 我在iOS-PHP应用程序中做了一个与众不同的功能。

(Of course userId and session is in the get parameter also) (当然,userId和会话也位于get参数中)

Calling like.php?id=10 does two things 调用like.php?id = 10有两件事

  • checks if the content 10 is liked already, 检查内容10是否已被喜欢,
  • if not then adds a row to the database's table 如果没有,则向数据库表中添加一行

Calling unlike.php?id=10 removes the like the user has made. 调用different.php?id = 10会删除用户所做的类似操作。

————— -----

The problem is, that sometimes if the user likes-unlikes the given content too fast, my table will receive 2 rows of the given like. 问题是,有时如果用户喜欢-不喜欢给定内容的速度太快,我的表将收到两行给定喜欢的内容。

———— ----

I assume it is because two requests to the like.php file happens exactly at the same moment.. 我认为这是因为对like.php文件的两个请求恰好同时发生。

What could I do to prevent multiple rows from same user? 我该怎么做才能防止来自同一用户的多行?

Use UNIQUE index in your database's table for user's id and content's id. 在数据库表中使用UNIQUE索引作为用户ID和内容ID。 The database will not insert a same user with same content in the like table. 数据库不会在同类表中插入具有相同内容的同一用户。

For me, 2 things: 1. Disable option for clicking button until request is finished (thanks to simple JS) 2. Lock the table for actions but keep it visible for reading for time of performing query. 对我来说,有两件事:1.禁用单击按钮的选项,直到请求完成(由于使用简单的JS)2.锁定表以执行操作,但保持表可见以供执行查询时读取。

you can even use both options so you will avoid having it clicked at the same time by 2 different users. 您甚至可以同时使用这两个选项,因此避免了2个不同用户同时单击它的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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