How to store correct format to db using php

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

  1. #1
    Hi Every body...

    I make this script with mysql query but it has problem..
    i want to enter icnumber format like below

    Format is like this (810605-14-6356)
    This is the rite format, No a to z letters...
    6 digits then - then 2 digits then - then 4 digits.

    Code is here
    
    dbConnect('db');
        
        $ic = $_POST['icnumber'];
        
        if (preg_match('#^[0-9]{6}\-[0-9]{2}\-[0-9]{4}$#', $ic)) { 
        //$ic is valid
        error ('IC Number is not in right format.\\n'.
                  'Please enter IC Number agian.');
         }
              
       if ($ic==''
          or $_POST['cicnumber']=='' )  { 
             error ('One or more required fields were left blank.\\n'.
                  'Please fill them in and try again.');
        }
        // this makes sure both passwords entered match
       if ($ic != $_POST['cicnumber']) {
       error ('Your IC Numbers not matched.\\n'.
                  'Please try again.');
       }
         
        // Check for existing user with the ic number
        $sql = "SELECT COUNT(*) FROM m_users 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.');
        }
        
            
        $random = rand(1000000,9999999990);  
        
        
        $sql = "INSERT INTO m_users SET
                  icnumber = ('$ic'),
                  acccode = ('$random'),
                  actdate = curdate(),
                  expdate = DATE_ADD(curdate(), INTERVAL 1 month);
                  ";
        if (!mysql_query($sql))
            error('A database error occurred in processing your '.
                  'submission.\\nIf this error persists, please '.
                  'contact admin@straight-a.com.my .\\n' . mysql_error());
                
        ?>
    
    Code (markup):
    I make this query for that to check the icnumber format but it not works..

    The below is the code by which i want to check the icnumber format..
    
    $ic = $_POST['icnumber'];
        
        if (preg_match('#^[0-9]{6}\-[0-9]{2}\-[0-9]{4}$#', $ic)) { 
        //$ic is valid
        error ('IC Number is not in right format.\\n'.
                  'Please enter IC Number agian.');
         }
    
    Code (markup):
    Can any one help me out from this problem...
    Thanks in advance..
     
    mmarif4u, Jan 3, 2007 IP
  2. kjewat

    kjewat Active Member

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    The php code (and the regex) seems to be correct.

    The only error I see is when you check for existing Ic numbers:
    I think you will get an error if your don't use single quotes ' around $ic, like this:
    $sql = "SELECT COUNT(*) FROM m_users WHERE icnumber = '$ic'";
    PHP:
    But your error function would be called, so I guess you would have seen the error...
     
    kjewat, Jan 4, 2007 IP
  3. mmarif4u

    mmarif4u Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks kjewat for ur help..

    I make some changes in it and now its working.

    
    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):
    its working fine..
    i appreciate ur help..
     
    mmarif4u, Jan 4, 2007 IP