In the intranet system, when a ticket number is submitted, repeat checks are performed, but the same ticket number is submitted twice. Intranet system,
Server PHP + MYSQL
When the data table field value is written into PHP, it is normal to prompt that the same ticket number cannot be inserted repeatedly, but sometimes it is only submitted once during operations, however, it is found that the data is identical and two data entries are written in the same second at the same time.
The man knows what's going on.
Reply to discussion (solution)
It is estimated that the request was submitted twice.
I currently have two restrictions on this situation: front-end and back-end.
When you click the submit button on the front-end, the submit button becomes grayed out and cannot be clicked. Then, after ajax transmits a value to the background and returns the result, the program logic is switched to or restored.
Before inserting data in the background, determine whether the data already exists (based on one or more fields that cannot be duplicated.
Yes. if the form is submitted, you can also use js to replace submit. after clicking the submit button, the js will disable the button first, and then use js to submit.
Only once, but this probability will happen once or twice a month. in my PHP code, I first query whether the current ticket number exists. if it does not exist, I can execute the job that writes data to the database. So what is the tangle...
It depends on the code you write to solve the problem from the source.
Second concurrency issues.
For example, two requests, A and B, arrive at the same time.
AB is used to determine whether to repeat the position. After A is inserted, B will be inserted.
You can write it in this way to avoid repeated inserts.
For example, the test table has two fields: name and value.
INSERT INTO test(name, value) SELECT 'fdipzone', '123' FROM DUAL WHERE 'fdipzone' NOT IN (SELECT name FROM test);
Since it is an intranet system, the concurrency is certainly not high, and the probability of multiple users clicking the button at the same time is lower.
Do a good job of front-end to prevent repeated submission. you can submit the verification in the background.
Second concurrency issues.
For example, two requests, A and B, arrive at the same time.
AB is used to determine whether to repeat the position. After A is inserted, B will be inserted.
You can write it in this way to avoid repeated inserts.
For example, the test table has two fields: name and value.
INSERT INTO test(name, value) SELECT 'fdipzone', '123' FROM DUAL WHERE 'fdipzone' NOT IN (SELECT name FROM test);
I think your writing method should be recommended and you should judge it again in the insert statement.
Although the probability that multiple users submit the same content at the same time is very low
This problem often occurred 10 years ago when I started programming. The reason is that the upgraded version of the browser is unstable, resulting in one request sending data twice. of course, it has been fixed now. However, it would be normal if the old user had to use it.
Another case is that the form is not submitted to this page, or the submission is completed by ajax. if the user's mouse is too tired, the single-click mode is changed to double-click mode. It is also possible to cause secondary submission.
To prevent these non-human faults, you can make some judgments when receiving them.
Session_start (); if (isset ($ _ SESSION ['post']) & $ _ SESSION ['post'] = md5 (serialize ($ _ post ))) return; $ _ SESSION ['post'] = md5 (serialize ($ _ post); // normal submission