Each time a user types a word into a text field, I call a function that writes the word to the database.

table:
id | word | category

Now if user paging the results, the operation repeats and the results duplicate in the database.

Is there a way to not duplicate the results?

Checking the word and category in the database will not give the expected result, because they can be entered today, yesterday, the day before yesterday.

Date and time also, because two users can enter the same words in a short space of time.

1 Answers

1
Ashley On Best Solutions

As Dharman said in their comment, you can add a Unique key constraint. However, are you saying that the same word can be saved by different users and at different times? Or can the word only be saved once, no matter what time, and no matter which user?

Also, just using the Unique key will work functionally, but what is the user experiencing? What do you want the user to experience? Do you want the user to receive a clear error message about what happened? If so, you will need to do validation which will require a query to check.