I wanted to create an automatic numbering record but would reset every year. For example, 170001 ... 170002 and for the year 180001 ... 180002
But with this code I am always registering 1700001
<?php
function conectarBanco(){
return new mysqli('localhost', 'root', '', 'bvmarco');
}
function primeiraOrdemAno(){
return date('y'). '00001';
}
function novaOrdemServico(){
$db = conectarBanco();
$sql = 'SELECT max(Id) as 'ultimo_id' FROM 'participacao' WHERE year(data) = year(now()) ';
$result = $db->query($sql);
if($result === true){
$ordem_servico = $result->fetch_assoc();
return ++$ordem_servico['ultimo_id'];
}else{
return primeiraOrdemAno();
}
}
$nova_ordem = novaOrdemServico();
?>
<input type="hidden" name="id" class="form-control" value="<?php echo $nova_ordem ?>" required>
With help I was able to do the code. When you need to, here's a tip.
<?php
function conectarBanco(){
return new mysqli('localhost', 'root', '', 'bvmarco');
}
function primeiraOrdemAno(){
return date('y'). '00001';
}
function novaOrdemServico(){
$db = conectarBanco();
$sql = 'SELECT max(Id) as 'ultimo_id' FROM 'participacao' WHERE year(data) = year(now()) ';
$result = $db->query($sql);
if($result === true){
$ordem_servico = $result->fetch_assoc();
return ++$ordem_servico['ultimo_id'];
}else{
return primeiraOrdemAno();
}
}
$nova_ordem = novaOrdemServico();
?>