I am using PHP and MySQL database.
In a scenario were admin and student both hits database at a same time(milliseconds) and my database table gets duplicate record.
Example: I have a table name Student_payment with 3 attributes. ID(PK), name, token_number. Both admin and student updates same table and duplicate token number generated in student payment table. Here admin and student both are using different PHP pages i.e. student_admin.php and student using student.php.
This is a scenario where few students visit college office, pay the fees manually (cash, cheque, UPI), request the admin to enter data and admin enters the data. Let’s assume this student name is Student_Y
Other students let’s assume student name as Student_X pay the fees online using online payment gateway and this data is inserted automatically to student_payment table after the payment.
In some case within fraction of second they(admin and student) hit the same button and duplicate token_number generated in student_payment table. This happens when student_X uploads his data through his/her mobile or laptop and admin uploads student_Y data in office simultaneously. Both are different data but same token number is assigned for both.
token number is generated in both student_admin.php and student.php page. At present, we get the max(token_number) and increment by 1 and then insert the data immediately to student_payment table.
Lock database solution may not work since admin gets the first control and table is locked then student(student.php) if he/she does online payment then partial transaction is done. payment is received and data is not inserted after payment.
Is there any solution in PHP or from MySQL database to solve this?