0

I'm developing a web application with PHP and MySQL in that I have a situation where I have to limit the number of records to be inserted in a table.

...
const MAX= 10
if(/*record count query*/ < $this::MAX) {
 /*insert query*/
}
...

For test purpose I'm triggering this code just by using GET request from the browser.

When I click F5 Key(refresh) continuously for about 5 seconds the count exceeds the MAX.

But when I go one by one the count is with in the limit.

This shows that when I click F5 continuously count query got executed while the insert query is executing simultaneously. I have no idea on how to solve this problem some guidance would be helpful to me.

1 Answer 1

1

You have to LOCK the table so no other process writes to the database when you are trying to get the current count. Otherwise you are always under the risk that another process currently inserts the data.

For performance reasons, you may use another table just as a counter, which you will lock during those operations.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting Error Number: 1100 Table 'example' was not locked with LOCK TABLES
Note: The query will do read from one table and insert into other table. So, I used many nested queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.