I am working on project with php-MySQL in the back-end.It has various articles to browse,sorted into various categories and a user-login system.
I recently added a watch-list functionality to it which enables users to add articles to their watch-list/dashboard.
I have the following tables.
Articles table
article-id(int) Primary-Unique
cat(varchar)
name(varchar)
subCat(varchar)
description(varchar)
date added
users table
username(varchar) Primary-Unique
lname
fname
joined-date
.Watchlist table
article-id(int)
username(varchar)
date_watch
As we can see there is no unique key for watch-list table
I want to make (username + article-id) a unique pair
Because for every username more than 1 article-id's exist and viceversa
I just want to insert and delete rows and It's not needed to update them
how to prevent duplicate entries?
till now I have been checking number of rows with php
"SELECT * FROM watchlist WHERE article-id={$id} AND username={$user}"
and if
mysql_num_rows() >1(not allowed to insert)
This works fine but if by mistake that INSERT command is executed thare will be a duplicate entry
How to prevent it?
I am using phpmyadmin