How do I put extra number at the end of username if duplicate

前端 未结 8 1890
醉话见心
醉话见心 2021-01-19 23:31

Hard to explain, and let me show an example.

  1. If username foo already exists in MySQL I want php script will allow it but to be foo1
8条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-20 00:26

      $getname   = $_POST['getname'];
    
      $i++;
    
      $q =  "SELECT username FROM User WHERE username LIKE '".$getname."%'";
    
      $r = mysql_query($q);
    
      if(mysql_num_rows($r) > 0){
    
        while($row = mysql_fetch_object($r)){
          if($row->username == $getname){
            $uname = $getname.$i;
          }else if($row->username == ($getname.$i)){
            $uname = $getname.$i+1;
          }
          $i++;
        }
    
      }
      else
      {
       $uname = "foo";
      }
    

提交回复
热议问题