0

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] => 211 [:db_insert_placeholder_1] => 82916 [:db_insert_placeholder_2] => 1 [:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => 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->nid == 210) {
     
  $node_out = node_load('211');
  global $user;

  $data = array(
    'first_name' => $submission->data[3][0],
    'last_name' => $submission->data[4][0],
    'email' => $submission->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' => '211',
    'uid' => $user->uid,
    'sid' => NULL,
    'submitted' => REQUEST_TIME,
    'completed' => REQUEST_TIME,
    'remote_addr' => ip_address(),
    'is_draft' => FALSE,
    'data' => $data,
  );
  
  $number_of_tickets = $submission->data[24][0];
  while ($number_of_tickets > 0) {
    webform_submission_insert($node_out, $submission_out);
    $number_of_tickets -= 1;
  }
}
}

1 Answer 1

0

Not entirely sure why, but moving the line that sets $submission_out inside the loop fixes this problem.

1
  • 1
    It's because webform_submission_insert() alters $submission_out - it sets the sid. An alternative solution would be $submission_out['sid'] = NULL; in the loop
    – Clive
    Apr 10, 2021 at 19:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.