whats wrong ??? (function)

Discussion in 'PHP' started by Shiftil, Mar 13, 2009.

  1. #1
    i have a function for register users

    now i what to do, if the username existing
    the function give to me a message

    i have the register function
    
    function register($username,$password,$firstname,$lastname)
                      {
                          // just  check //
                          $check = mysql_query("SELECT * FROM `test` WHERE username='".mysql_escape_string($username)."' LIMIT 1;");
                          if(mysql_num_rows($check) != 0)
                            {
                             return false;
                            }
                          if (!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['firstname']) && !empty($_POST['lastname']))
                           {
                                //smart encoding //
                                $chars = "!a@b$g%^&*()_+=-'\";][\\/:87{}~`";
                                 for($i=0;$i<10;$i++)
                                     {
                                       $smart .= $chars[rand() % strlen($chars)-.07];
                                     }
    
                                 // do the pass//
                                $password = MD5($smart.$password.$smart);
    
                                if(mysql_query("INSERT INTO `test` (`id`,`username`,`password`,`smart`,`firstname`,`lastname`,`time`)
                                                VALUES (NULL, '".mysql_escape_string($username)."', '".$password."', '".$smart."', '".$firstname."', '".$lastname."',UNIX_TIMESTAMP())"))
                                  {
                                   return true;
                                  }
                                return false;
                            }
                          else
                            echo 'plz full all the fild!';
                      }
    
    Code (markup):
    and the register page:
    
    
    <?php
    
    if (isset($_POST['check_if_press']) && $_POST['check_if_press'] == "Send")
              {
                  if ( $_POST['password'] == $_POST['confrim'] )
                    {
                      if(register ($_POST['username'],$_POST['password'],$_POST['firstname'],$_POST['lastname']) == 'false')
                      echo 'username existing ';
                      register ($_POST['username'],$_POST['password'],$_POST['firstname'],$_POST['lastname']);
                      if(mysql_insert_id())
                       {
                       echo "you are signed in, thx you for registering <br /> ".$_POST['username'];
                       }
                    }
                  else echo ' your password and confrim not same';
                }
    
    
    
     ?
    
    Code (markup):
    plz help me to do that :/
     
    Shiftil, Mar 13, 2009 IP
  2. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This is the function that check whether the username is previously available or not..if the username exists then it will return 1 else it will return 0
    
    function checkUser($memberName)
    {
    $selectQuery = "SELECT COUNT(*) AS cnt FROM test WHERE userName='$memberName'";
    $resultQuery = mysql_query($selectQuery);
    $rowQuery = @mysql_fetch_array($resultQuery);
    return $rowQuery['cnt']
    }
    
    PHP:
    Now After you submit the forms..

    
    if(isset($_POST['registerUser']))
    {
    $userName = $_REQUEST['userName'];
    $password = $_REQUEST['password'];
    $firstName= $_REQUEST['firstName'];
    $lastName= $_REQUEST['lastName'];
    $time= date("Y-m-d");
    ////////////Checking for existing user///////////
    $userAvailableStatus = checkUser($userName);
    if($userAvailableStatus == 1)
    {
    echo("<script>alert('User name already taken')</script>");////////////Gives alert message when name is already taken
    echo("<script>location.href=''</script>")//////////Redirect according to your wish
    
    }
    else
    {
    $insertQuery = "INSERT INTO test SET username = '$userName',firstName = '$firstName',lastName='$lastName',time='$time'";
    mysql_query($insertQuery);
    echo("<script>location.href=''</script>")//////////Redirect according to your wish
    }
    }
    
    PHP:
    Cheers
    Stylesofts Developing Team
     
    Stylesofts, Mar 13, 2009 IP