Error trying to submit webform programatically multiple times: Duplicate entry for key 'PRIMARY' I'm trying to programatically send submissions to a webform n times, but when I submit the submission info to the webform for the second time, I get an Integrity constraint violation: 1062 Duplicate entry 'nnnnn' for key 'PRIMARY' error. It appears the second submission has the same sid as the first submission, but I'm not sure why as I'm submitting NULL for the sid. How can I submit the webform again with the same information, but get a new sid? Drupal 7.
Here is the error in full:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '211-82916-1-0' for key 'PRIMARY': INSERT INTO {webform_submitted_data} (nid, sid, cid, no, data) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4); Array ( [:db_insert_placeholder_0] =&gt; 211 [:db_insert_placeholder_1] =&gt; 82916 [:db_insert_placeholder_2] =&gt; 1 [:db_insert_placeholder_3] =&gt; 0 [:db_insert_placeholder_4] =&gt; Bob ) in drupal_write_record() (line 7471 of /var/www/html/includes/common.inc).
And the code:
function wildsight_raffle_tickets_webform_submission_insert($node, $submission) {
  if ($submission-&gt;nid == 210) {
     
  $node_out = node_load('211');
  global $user;

  $data = array(
    'first_name' =&gt; $submission-&gt;data[3][0],
    'last_name' =&gt; $submission-&gt;data[4][0],
    'email' =&gt; $submission-&gt;data[14][0],
  );

  module_load_include('inc', 'webform', 'webform.module');
  module_load_include('inc', 'webform', 'includes/webform.submissions');
  
  // This methods will arrange $data in the right way
  $data = _webform_client_form_submit_flatten($node_out, $data);
  $data = webform_submission_data($node_out, $data);

  $submission_out = (object) array(
    'nid' =&gt; '211',
    'uid' =&gt; $user-&gt;uid,
    'sid' =&gt; NULL,
    'submitted' =&gt; REQUEST_TIME,
    'completed' =&gt; REQUEST_TIME,
    'remote_addr' =&gt; ip_address(),
    'is_draft' =&gt; FALSE,
    'data' =&gt; $data,
  );
  
  $number_of_tickets = $submission-&gt;data[24][0];
  while ($number_of_tickets &gt; 0) {
    webform_submission_insert($node_out, $submission_out);
    $number_of_tickets -= 1;
  }
}
}
Not entirely sure why, but moving the line that sets $submission_out inside the loop fixes this problem.