March 7, 2012

FInding duplicates in db values

Question by santa

I have a table with the following fields:

id
project
projectName

There are two different way when I display this data:

  1. Before I store it into a database I place projectName values into text fields. In this case I need to be able to somehow mark when there are duplicated with some sort of jQuery code, and

  2. I output the values from db into a list. Again, I need to somehow catch duplicates and mark them somehow, perhaps changing text color.

There could be multiple duplicate sets.

How can I do that?

Answer by Starx

Ok, you can send an ajax request to the server and see if is actually duplicate. I will give a simple example of how we can get something like this to work.

HTML

<a id="check"> check availabilty</a>

JQuery

$("#check").click(function() {
    $.post("checkname.php", 
           { name : $("#textboxname").val() },
           function(data) {
             //data will contain the output from the file `checkname.php`
             if(data=="ok") { //imagine the case it output ok if not duplicate is found
                alert('ok');
             else { 
                alert('duplicate name exists');
             }
    );
});

PHP checkname.php

$name = $_POST['name'];
//now confirm this with a query or just use your own logic
//
//

if($resultfound) { echo "ok"; } else { echo "no"; }

Note: This is a very basic example to illustrate the process.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!