title: Limit concurrently sql queries for one user
body: <p>I have a strange problem:</p>
  
  <p>Basically I developed a PHP Form that when is submitted by an user the form will proccess an INSERT query with multiple values:</p>
  
  <pre><code>INSERT INTO tbl_name
          (id, colB, colC)
   VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?) [,...]
  </code></pre>
  
  <p>So far there is no problem. The problem is that for a couple of reason (not relevant) I have to insert manually the ID of the rows that I'm going to insert so before this query I'm going to make also this query:</p>
  
  <pre><code>SELECT MAX(id) FROM tbl_name WHERE id &lt; x
  </code></pre>
  
  <p>So I can take the id where to start my query.</p>
  
  <p>Specifying that the queries will take more than a normal query because they're queries in an external database, it takes about 2-3s for the INSERT query.</p>
  
  <p>The problem is:</p>
  
  <p>If one user is going to submit the form, he take the reference ID, for example <code>45</code> and then the INSERT queries are processed:</p>
  
  <pre><code>INSERT INTO tbl_name
          (id, colB, colC)
   VALUES (45, 2, 2), (46, 5, 7), (47, 1, 4) [,...]
  </code></pre>
  
  <p>But during the INSERT query another user submit the form and he take as reference ID one of the ID of the rows which is processing the other user.</p>
  
  <p>How can I avoid this problem? I thought about limit concurrently sql queries for one user that I will use just for the website, but I don't have any idea how to do it. There's other solutions? Or maybe MySQL it already has a prevention to this problem? Thanks.</p>