• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

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.
0
genesisvh
Asked:
genesisvh
  • 2
1 Solution
 
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:
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now