• 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.
  • 2
1 Solution
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.
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';
echo "$value already found in database";
//run your insert query here

Open in new window

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

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.

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