PDA

View Full Version : how to avoid duplicate entry in database


maham
08-07-2011, 01:18 PM
hello .. can anyone help me in inserting a duplicate entry in database?

Actually i have a table "option" with 3 fields and two of them are set to primary key (composite). I have a poll descriptions in some other table (not concern here) and i have to put that poll's answers in table "option" and obviously when i insert data i should be like this

poll_id answer_id answer_des

1 1 myans_1
1 2 myans_2
1 3 myans_3

the above data means that there is a poll with id=1 and it has 3 answers.
first row is inserted easily but on second time it shows error of duplicate entry .. howdo i avoide it because i cannot remove the tag of primary key from "poll_id" and "field_id" .. Is there is any solution?

Fou-Lu
08-07-2011, 02:39 PM
It sounds to me that the composite key is not actually in place, and only the poll_id is the primary.
Run a describe on the table in question. Does it list that both the poll_id and answer_id are of type PRI for their keys? If these check out and list that the composite is in place, then the problem is you simply already have a record of 1 and 2 in the database before inserting OR your code is not updating the answer_id correctly before trying to insert it so that the second entry is trying to go in as 1 and 1 instead of 1 and 2.


I just noticed this is in the other databases forum. What database are you using; DESCRIBE is a proprietary MySQL command.