Calculate ID coming from Database

0

I'm doing maintenance on a PHP system with Mysql. I noticed that in the user table there is a field called code. This field stores as follows:

id_usuario | codigo_usuario
   1            00001

And the last value:

   id_usuario | codigo_usuario
       7810         07810

As I could calculate, can it be in PHP or within the table itself so that the next records maintain this pattern? Ex:

  id_usuario | codigo_usuario
     7811          07811
    
asked by anonymous 30.11.2018 / 13:55

1 answer

1

You can do this in your php as follows:

1) insert it into your bank

2) Then get the last id inserted with the function below

'$ultimoId= mysqli_insert_id($con)';

3) then place the leading zeros with the following cod:

$cod_usuario=sprintf("%05d", $ultimoId);

4) Make an update to your table where you are the $cod_usuario where id is $ultimoId

    
30.11.2018 / 14:35