1

I have a table in the database contains ID which is the primary key and auto incremented, and RFC the RFC should be auto incremented as well like RFC-0001 then RFC-0002 but on the HTML/PHP part for some reason I'm doing something wrong and I dont know how to fix it any ideas ? this is my database table

enter image description here

and this is my php html code

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "change";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

?>
<?php
$try="select * from test order by id desc limit1";
$result= mysqli_query($conn,$try);
$row=mysqli_fetch_array($result);
$lastid= $row['rfc'];
if ($lastid == " ")
{
 $empid ="RFC-0001";
}
else
{
 $empid = substr($lastid, 4);
 $empid= intval($empid);
 $empid= "RFC-".($empid +1);
}
?>
<html>
<body>
<form method="post" action="">
<input type="text" name="ID" value="<?php echo $empid; ?>"readonly>
</br>
<input type="text" name="name">
</br>
<input type="button" name="done" value="done">
</form>

</body>
</html>


programming freak
  • 859
  • 5
  • 14
  • 34

1 Answers1

1

Replace

$empid= "RFC-".($empid +1);

with

$empid= "RFC-".sprintf('%04d', ($empid +1));

sprintf() allows padding a number with zeros

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100