title: MySQL - Return generated AUTO_INCREMENT value on INSERT
body: <p>Hopefully an easy one - I can't work out a robust solution.</p>
  
  <p>I'm building a cart mechanism which generates a id_cart AUTO_INCREMENT Primary Key. The cart item has another table with cart product variables like size. I want to attribute the same id_cart in the size table.</p>
  
  <pre><code>CREATE TABLE
  -&gt; id_size_cart NOT NULL PRIMARY KEY AUTO_INCREMENT,
  -&gt; id_cart //THIS WILL BE THE AI value from the product cart
  -&gt; size_name
  </code></pre>
  
  <p>I've seen SELECT LAST_INSERT_ID().</p>
  
  <p>How solid will this be to ensure it's the last inserted ID? Don't want customers getting the wrong size!</p>
  
  <p><em><strong></em>__<em></strong> EDIT <strong></em>___</strong></p>
  
  <p>Thank you for the input. Still, I'm having trouble. See actual code below;</p>
  
  <pre><code>//add product to cart table
  $addtocart_sql = "INSERT INTO store_cart (id_cart, id_session) VALUES ('', '".$_COOKIE["PHPSESSID"]."')";
  $addtocart_res = mysqli_query($dbConnect, $addtocart_sql) or die(mysqli_error($dbConnect));
  
  $last_row_id = mysqli_query($dbConnect, "SELECT LAST_INSERT_ID()") or die(mysqli_error($dbConnect));
  
  echo 'INSERT worked!&lt;br/&gt;';
  echo $last_row_id;
  </code></pre>
  
  <p>Error shows as</p>
  
  <blockquote>
    <p>Parse error: syntax error, unexpected T_STRING in /websites/LinuxPackage05/4v/35/xy/4v35xy-55415.webfusion-hosting.co.uk/public_html/noff-group.com/dev/add_to_cart.php on line 27</p>
  </blockquote>
  
  <p>Thanks in advance.</p>