php - Handle session while form submission
I have a web application where users fill the form and a unique id is generated like for example "SC001001". the code for generation is
$result = mysqli_query($con,"SELECT OA_NO FROM student where CUSTID='SC001'");
$x=0;
$num_rows = $result->num_rows;
if($num_rows==0){
$var1="SC001001";
}
else{
while($row = mysqli_fetch_array($result))
{
$next[$x]=substr($row['OA_NO'],2);
$x=$x+1;
}
$arrlength=count($next);
$inc=max($next)+1;
$newvar= "SC00".$inc;
$var1=$newvar;
}
While saving it in the database the OA_NO is the primary key.
but when two users press the submit button at the same time i get the error message #1062. Duplicate entry present and the entry of the first request gets inserted...
my question is how shall i enter both the values with incrementing the second one.. PLEASE HELP!!
Answer
Solution:
If you find the maximum value while doing the insert, the value should always be unique. I added the
LPAD
function in case you wanted to keep the length of userID consistent:Answer
Solution:
You shouldn't generate a primary key that way.
If you want a sequential numbering, you should use an auto-incremented field as primary key (or as part of the primary key).
Hope it helps