Double entries in MySQL Database

Is there a php script to check if the user has entered a duplicate entry in mysql row? Or least show me how to start the script or direct me to a good guide. Let say the someone is adding a description that is the same to another description.
genesisvhAsked:
Who is Participating?
 
lexlythiusConnect With a Mentor Commented:
Easier done in MySQL itself:
SELECT t.*, COUNT(*) AS duplicate_count 
FROM YourTable t
GROUP BY YourFieldThatShouldHaveAnIndex
HAVING duplicate_count > 1

Open in new window

0
 
mds-cosCommented:
A very simple soluion can be obtained at the MySQL database side.  Just set the field to require a unique value -- then an error will be generated if a duplicate value is entered.  Since you are needing unique values, I would look at the field as a good candidate to index as well.
0
 
Jagadishwor DulalBraces MediaCommented:
Again if you want to check the value exist or not than you should use select query and check the value exist or not like:
$q="SELECT * FROM tblname where fieldname='$value';
$r=mysql_query($q);
if(mysql_num_rows($r)>0){
echo "$value already found in database";
}else{
//run your insert query here
}

Open in new window

0
 
lexlythiusCommented:
My solution helps you when you want to find out existing duplicates.

@jagadishdulal solution works best if you have a user input you want to validate prior to inserting it into DB.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.