Double entries in MySQL Database

Posted on 2011-05-12
Last Modified: 2012-05-11
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.
Question by:genesisvh
    LVL 14

    Expert Comment

    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.
    LVL 15

    Expert Comment

    by:Jagadishwor Dulal
    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

    LVL 7

    Accepted Solution

    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

    LVL 7

    Expert Comment

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now