i working on project php-mysql in back-end.it has various articles browse,sorted various categories , user-login system.
i added watch-list functionality enables users add articles watch-list/dashboard.
i have 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 can see there no unique key watch-list table
i want make (username + article-id) unique pair because every username more 1 article-id's exist , viceversa
i want insert , delete rows , it's not needed update them
how prevent duplicate entries?
till have been checking number of rows php
"select * watchlist article-id={$id} , username={$user}"
and if
mysql_num_rows() >1(not allowed insert)
this works fine if mistake insert command executed thare duplicate entry
how prevent it?
using phpmyadmin
you can add unique key table:
alter table `watchlist` add unique index `watchlist_unique` (`article-id`, `username`);
also, looking @ functionality of have, may opt setting primary key, using instead of above:
alter table `watchlist` add primary key (`article-id`, `username`);
both prevent insertion of duplicate entry.
in addition, if want insert in case, may want check out insert ignore , on duplicate key. more info on both see "insert ignore" vs "insert ... on duplicate key update".