How to prevent multiple entries to db...

Discussion in 'PHP' started by mmarif4u, Jan 5, 2007.

  1. #1
    Hi everybody,,,
    I am facing problem,
    Problem is that i have a user with ic already have in my database.
    But my query enter another data to db when i try to enter that ic
    again.i want that only one ic exist in db.i make query but t only checks the
    format like 1234 or 5462313 not 123aa and 123333-12-1234
    I want to check the format like 123456-12-1234..
    My query is:

    
    // Check for existing user with the ic number
        $sql = "SELECT COUNT(*) FROM loguser WHERE icnumber = $ic ";
        $result = mysql_query($sql);
        if (!$result) {	
            error('A database error occurred in processing your '.
                 'submission.\\nIf this error persists, please '.
                  'contact you@example.com.');
        }
        
         
        if (mysql_result($result,0,0)>0)  {
            error('A user already exists with your chosen IC Number.\\n'.
                  'Please try your own IC Number.');
        }
        if (preg_match('#^[0-9]{6}-[0-9]{2}-[0-9]{4}$#', $ic))
        {}else{      
        error('IC Number format is not Valid.\\n'.
                  'Please try again.');
           }
    
    
    Code (markup):
    please any one has idea about it,,
    i think the error is here thats why it didnot check the format entered.

    
    if (mysql_result($result,0,0)>0)  {
    
    
    Code (markup):
    thanks in advance
     
    mmarif4u, Jan 5, 2007 IP
  2. solidphp

    solidphp Peon

    Messages:
    46
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could add a unique index for the icnumber column:

    ALTER TABLE `loguser` ADD UNIQUE (`icnumber`);
    PHP:
    That would give you a mysql error if when you tried to enter a duplicate.
     
    solidphp, Jan 5, 2007 IP
  3. technoguy

    technoguy Notable Member

    Messages:
    4,369
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    205
    #3
    I often use simple funda

    $cnt= mysql_num_rows($result);
    if($cnt>0)
    {
    echo "Exist";
    }
    else
    {
    
    }
    PHP:
    thanks
     
    technoguy, Jan 5, 2007 IP
  4. mmarif4u

    mmarif4u Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks guys for kind reply..
    Problem is solved
     
    mmarif4u, Jan 5, 2007 IP