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. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If that is the problem then you could try your query without the count and then use mysql_num_rows to find out whether an entry already exists.
     
    streety, Jan 6, 2007 IP
  3. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #3
    its much easier:

    when you do the insert, just use "INSERT IGNORE INTO whatever...", this will only insert new values, or use "REPLACE" instead of "INSERT INTO" - depends on what you wanna do. check the mysql docs for more info about this commands.
     
    falcondriver, Jan 7, 2007 IP