Adding a static prefix on an incrementing number

后端 未结 1 1445
闹比i
闹比i 2021-01-25 10:30

I\'ve created a column on my form named trackingcode and it increments every time you insert a new data.

Now I thought about adding a prefix and then the nu

相关标签:
1条回答
  • 2021-01-25 10:55

    Try with str_pad()

    After insert get last insert id.pass with generate function like this param

    generate(prefix,last_insert_id);
    

    Ex:generate("REC",1);//REC0001

    function generate ($value='',$inp=""){
           return $value.str_pad($inp,4, "0", STR_PAD_LEFT);
    }
    
    
    
      //Insert query
    
       $query = 'INSERT INTO request(req_date, reqname, date, empname, position, account, platform, processor, ram, monitor, phone, phonetype, headset, req_table, req_chair, approval, status) VALUES("'.$req_date_clean.'", "'.$reqname_clean.'", "'.$date_clean.'", "'.$empname_clean.'", "'.$position_clean.'", "'.$account_clean.'", "'.$platform_clean.'", "'.$processor_clean.'", "'.$ram_clean.'", "'.$monitor_clean.'", "'.$phone_clean.'", "'.$phonetype_clean.'", "'.$headset_clean.'", "'.$req_table_clean.'", "'.$req_chair_clean.'", "'.$approval_clean.'", "'.$status_clean.'"); ';
         if(mysqli_query($connect,$query)){
          $last_id = mysqli_insert_id($connect);
          $code = generate("REQ",$last_id);
        mysqli_query($connect,"update request set trackingcode='$code'  where reqnumber='$last_id'");
        //exec code : update tablename set trackingcode='$code'  where tableprimary_id='$last_id'
    }
    
    0 讨论(0)
提交回复
热议问题