Problem with auto generated Register number

I am generating registration number by using following code, but the issue is that when more than one browser open forms . for example two operator when open form at same time in their computers, then same code loaded on both forms, which create issues, (remember i cannot make it unique ID or primary key becuase in my situation duplicate code can be entered as per situation, but it should not be open in two forms at the same time. Can any one suggest me solution for that.

<?php 
$CI	= "L5";	//Example only
$CIcnt = strlen($CI);
$offset	= $CIcnt + 6;

// Get the current month and year as two-digit strings 
$month = date("m"); // e.g. 09 
$year = date("y"); // e.g. 23  

// Get the last bill number from the database 
$query = "SELECT patientno FROM iap2 ORDER BY patientno DESC"; 
$result = mysqli_query($conn,$query); 
// Use mysqli_fetch_assoc() to get an associative array of the fetched row 
$row = mysqli_fetch_assoc($result); 
// Use $row[‘patientno’] to get the last bill number 
$lastid = $row['patientno']; 	 

// Check if the last bill number is empty or has a different month or year
if(empty($lastid) || (substr($lastid, $CIcnt + 1, 2) != $month) || (substr($lastid, $CIcnt + 3, 2) != $year)) { 
	// Start a new sequence with 0001 
	$number = "$CI-$month$year-0001"; 
} else { 
	// Increment the last four digits by one 
	$idd = substr($lastid, $offset); // e.g. 0001 
	echo $idd;
	$id = str_pad($idd + 1, 4, 0, STR_PAD_LEFT); // e.g. 0002 
	$number = "$CI-$month$year-$id"; 
} 
echo $number; 
?>

Generate the number when you are processing the form and adding the record to the database. Do not have a number in the form.

1 Like

I seem to recall several people mentioning this potential problem when you first asked about this - exactly as @Drummin says above, don’t generate it before the form has been filled in, generate it when the information is being stored.

If you absolutely have to have it on display in the form, then you must insert it into the database when the form is drawn. You then have to think of a way to deal with abandoned forms if you don’t want gaps in the number sequence.

1 Like

yes i also remember you and sir @Drummin warn me when i asked this question before, But Now i really understand the issue when i faced this in real time

<?php
    // getting all values from the HTML form
    if(isset($_POST['submit']))
    {
        $regdate = $_POST['regdate'];   
   
        $name2 = $_POST['name2'];
  	$rel2 = $_POST['rel2'];
	$age1 = $_POST['age1'];
        $gender = $_POST['gender'];       	
	      
        $remarks = $_POST['remarks'];	
	$mble = $_POST['mble'];       
  	$abc = $_POST['abc']; 	
  	
	
	
    }		

    // database details
    $host = "localhost";
    $username = "thehospi_root";
    $password = "u1m1a1r1";
    $dbname = "thehospi_hmis2";

    // creating a connection
    $con = mysqli_connect($host, $username, $password, $dbname);

    // to ensure that the connection is made
    if (!$con)
    {
        die("Connection failed!" . mysqli_connect_error());
    }

<?php 
$CI	= "L5";	//Example only
$CIcnt = strlen($CI);
$offset	= $CIcnt + 6;

// Get the current month and year as two-digit strings 
$month = date("m"); // e.g. 09 
$year = date("y"); // e.g. 23  

// Get the last bill number from the database 
$query = "SELECT patientno FROM iap2 ORDER BY patientno DESC"; 
$result = mysqli_query($conn,$query); 
// Use mysqli_fetch_assoc() to get an associative array of the fetched row 
$row = mysqli_fetch_assoc($result); 
// Use $row[‘patientno’] to get the last bill number 
$lastid = $row['patientno']; 	 

// Check if the last bill number is empty or has a different month or year
if(empty($lastid) || (substr($lastid, $CIcnt + 1, 2) != $month) || (substr($lastid, $CIcnt + 3, 2) != $year)) { 
	// Start a new sequence with 0001 
	$number = "$CI-$month$year-0001"; 
} else { 
	// Increment the last four digits by one 
	$idd = substr($lastid, $offset); // e.g. 0001 
	echo $idd;
	$id = str_pad($idd + 1, 4, 0, STR_PAD_LEFT); // e.g. 0002 
	$number = "$CI-$month$year-$id"; 
} 
echo $number; 
?>

    // using sql to create a data entry query
    $sql = "INSERT INTO iap2 (regdate,  name2, rel2, age1, gender,    remarks,  mble, uname1)
 VALUES ('$regdate',  '$name2', '$rel2' ,'$age1', '$gender',    '$remarks', '$mble',  '$abc')"; 

   
  
    // send query to the database to add values and confirm if successful
    $rs = mysqli_query($con, $sql);
    if($rs)
    {
        echo "";
    }
  

    // close connection
    mysqli_close($con);

?>

That’s not going to work, you have a PHP close tag before you run the insert query. You also seem to generate $number but then not do anything with it in your insert query.

The trouble you’ll have with that code is that there is still time for it to go wrong - two users pressing “submit” at roughly the same time could run the first query and get the same answer. That’s why you must trap the duplicate entry (your patientno column must have a unique constraint, I think you added that before) and you must trap the error, then increment the number and try again.

You could also look at transactions in mySQL, that might help you in this scenario.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.