Generating a registration code help

In my one php registration form i have generated one code automatically and if multiple users registering on form then each registration should be on different code but right now its not working. Right now if only two users using same form, still it is registering same code for two records. Please tell me how to make it perfect for multiple users??? Following i am sharing my core php code, Please tell me whether it is correct or wrong and tell me the changes also:

<?php
  include_once('dbConnect.php');

      // Insert
     if(isset($_POST['Submit']))
    {								
	$url = trim($_POST['URL']);					
	$ptype = trim($_POST['HID']);
				
	$AcName = trim($_POST['LName']);
	$Description = trim($_POST['descr']);				
				
	//*** Start Transaction ***//
	mysqli_query($dbCon,"BEGIN");
				
	echo "<script> alert('Function Called!!!'); GenerateCode(); </script>";   // This is Javascript function called for code Generartion again	
				
	$Code =  trim($_POST['code']);	// Posted that Assigned code to variable	

// Insert Query				
		$Query = "insert into ma_cal(CAL_CahId,CAL_Code,CAL_Name,CAL_Description) values ('$ptype','$Code','$AcName','$Description')";
								
   $Result = @mysqli_query($dbCon,$Query);
												
       if(!$Result)
       {
	  //*** RollBack Transaction ***//						 
            mysqli_query($dbCon,"ROLLBACK");

	$DB_Error = "Database Error => ".mysqli_error($dbCon); 
		}			
		else
		{
	//*** Commit Transaction ***//						 
         mysqli_query($dbCon,"COMMIT");					
						
    $InsertSuccess = "Done";
		}
	
}  // If end


// This is Javascript function for Code Generation which i have called in <Head> tag

 <script type="text/javascript">
	function GenerateCode()
	{
	var id = document.getElementById("HeadAcc").value;
			
	  var dataString = 'PID='+ id;
		
	$.ajax
	({
	    type: "POST",
	    url: "get_LedgerCode.php",
	   data: dataString,
	   cache: false,
	   success: function(html)
	   {					
	     $("#GotCode").html(html);
             } 
	});
	
   }	
	</script>

What does that have to do with “About Begin, Commit and RollBack in Core PHP Mysqli”?

Why are you generating any codes on the front end? The database should do that on insertion.

Hello sir,
As per you can see the code i have generated one ledger code and that should be unique for each new insert record so after pressing submit button, it is posting all values of textbox including that “Generated Code” so i have called that GenerateCode() function again to create unique code at the time of Begin and Commit.

         Please give me guideline that whether i have put that Begin and Commit at right place and how can i generate that unique ledger code for each insert record even if multiple users inserting record at a time??? Also, please tell me is there any another trick for this concept???

         Waiting for your reply.

Thanks & regards,
Sagar Gavali.

This is all kinds of wrong.

And you don’t need a transaction for what you’re doing. Don’t know why you think it is needed, because it appears you don’t know what it is for.

1 Like

Hello sir,
Please find the attachment, This is my form in which Code (EXP001003) has generated by taking some prefix and last MAX record from ledger table. I have entered Name i.e. EMI so before me submitting this form if other user inserted 3 records then Code should be EXP001006 so that at the time of Insert query i have again called that GenerateCode() function to avoid duplicate code entry. I cannot put Unique constraint in my database for this field. So, Please tell me instead of my published code is there any another solution for me???

You would just INSERT the submitted form data and if you need to get the id of the record just inserted for some purpose, get the last insert id. The database extension you are using has a property that returns the last insert id. The id column needs to be an auto-increment primary index. Any formatting of the value, such as the “EXP”+leading zeros, would be done when you display the value, not as part of the stored value.

Database Transactions are used when you have multiple data modification queries that must all succeed or they must all fail and roll back the data to the starting point. A Transaction is NOT used for a single data modification query.

You should NOT put external/unknown data directly into an sql query statement. Use a prepared query instead, with ? place-holders for each data value, then supply the data when the query gets executed. Unfortunately, the php mysqli extension you are using is overly complicated and inconsistent when dealing with prepared queries and you should switch to the much simpler php PDO extension.

What @phdr said, he just got to it first…

This is actually far simpler than you are making it.

$id = mysqli_insert_id($dbCon);

Don’t do this EVER! The @ suppresses errors. Why would feasibly NOT WANT TO KNOW SOMETHING HAPPENED???

Thank you very much for your reply but Last Inserted Id will not be useful here. @astonecipher and @phdr

Yes, that is what you use. That is basically what the below does, just incorrectly.

Sponsor our Newsletter | Privacy Policy | Terms of Service