As a welcome toast will an answer, in your next questions follow the post I recommended in the comment of your question
//conexão ao banco
$conn = new mysqli("localhost", "USUARIO", "SENHA", "nome_DB");
//Letras dos setores recuperadas via post do formulário
$centro=$_POST["centro"];
/*seleciona um registro (limit 1) cujo as 4 primeiras letras são iguais
a variável acima na ordem descendente da coluna tag, que significa dizer, o maior,
pois a parte das letras com tracinho são iguais e o que vai prevalecar na ordenação é
a parte numerica.******/
$query = ("SELECT tag FROM cadastro WHERE LEFT(tag,4)='$centro' order by tag DESC limit 1");
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_assoc($result)) {
//o registro
$tag = $row["tag"];
//separamos a parte numerica
$num=substr($tag,5);
}
/*****caso haja registro insere incrementando, caso contrário
vai pro else inserir o primeiro******/
if ($num!=""){
//somamos 1 a parte numerica
$proximo=($num+1);
/**retorna o comprimento da variável acima, necessário pq o PHP ao somar 0001 + 1
retorna 2, 0012 + 1 retorna 13, 0155 + 1 retorna 156 etc... dai
precisamos saber quantos zeros a esquerda serão necessários***/
$comprimento=strlen($proximo);
//quantidade e zeros necessários
$zerosEsquerda = "4-$comprimento";
// função responsável por colocar os zeros à esquerda
$num = str_pad($proximo, $zerosEsquerda, '0', STR_PAD_LEFT);
// preparação do value da declaração insert
$strProximo= $centro."-".$num;
$conn->query("Insert into cadastro (tag) values ('".$strProximo."')");
}else{
$primeiro=$centro."-0001";
$conn->query("Insert into cadastro (tag) values ('".$primeiro."')");
}
Post the structure of your database, you probably won’t even need to do anything else, depending on how you are, you keep this data (acronym-number) in different columns and when you search or show the user just join
– Costamilam
Welcome Ronaldo Souza, here is a post to get you successful in your next questions. How to create a minimal example https://answall.com/help/mcve. It is also important to mark how you accept a response that has solved your problem. Here’s how to mark a response as it accepts https://i.stack.Imgur.com/evLUR.png and here’s why to accept a response
– user60252