0

i am having trouble storing webform component values into a custom database table, i have created a custom module to insert the data into the table but its not working and i can't figure out where my problem is can someone please help here's my code thanks

<?php 
function customform_form_alter(&$form, $form_state, $form_id) {
    if ($form_id == 'webform_client_form_24') {
        $form['#submit'][] = 'customform_additional_insert';
        if ($node = menu_get_object()) {
            $form['submitted']['node_id']['#value'] =$node->nid;
            $form['submitted']['amount']['#value'] = @$node->field_needed_amount['und'][0]['value']; 
            $form['submitted']['full_name']['#value'] = @$node->title;
            $form['submitted']['image']['#value'] = @$node->field_student_photo['und'][0]['fid'];  
        }
    }
}

function customform_additional_insert($form, &$form_state) {
    $data = $form['submitted'];

    // Insert a record into a 3rd-party module table when a submission is added.
    db_insert('sponsorship')
        ->fields(array(
            'n_id' => $form['submitted']['node_id']['#value'],
            'user_id' => 1,
        ))
        ->execute();
}
?>
2
  • I can't help but feel that this is duplication of data and should be avoided. Why do you need to store the data in a separate table? Can't whatever needs its take the data directly out of the webforms tables?
    – Felix Eve
    Mar 25, 2014 at 16:57
  • thanks for the response Felix. thats what i tried first to get it from the webform submission data table but i could not get it. can you help with that
    – Baboucarr
    Mar 25, 2014 at 17:17

2 Answers 2

1

I would suggest taking the data straight out of the webforms DB tables.

To understand how to do that we need to look at how data is stored in the DB.

Each time a new webform is created a row for it is created in the webform table. Each webform has a unique identifier called nid.

Each component added to the webform is stored in the webform_component table and has a unique identifier of cid (cid is only unique per webform, nid is also required to make the primary key).

Now when a user fills in the webform a row is created in the webform_submission table (with a unique id of sid) and each fields value is stored in webform_submitted_data. Each row in the webform_submitted_data table has a nid (the webform ID), sid (submission ID) and cid (the component ID).

enter image description here

So, if you wanted to get all of the data from a particular submission with the name of the field (or component), uid and the data submitted you can use the following query:

SELECT c.`name`, sd.`data`, s.uid
FROM webform_submitted_data sd
INNER JOIN webform_component c ON c.cid = sd.cid AND c.nid = sd.nid
INNER JOIN webform_submissions s ON s.sid = sd.sid
WHERE sd.sid = {YOUR SUBMISSION ID}

Or to show all of the values submitted for a particular field with uid and date then you could use:

SELECT sd.`data`, s.uid, s.submitted
FROM webform_submitted_data sd
INNER JOIN webform_component c ON c.cid = sd.cid AND c.nid = sd.nid
INNER JOIN webform_submissions s ON s.sid = sd.sid
WHERE sd.cid = {YOUR COMPONENT ID}
0

I know this is a bit old, but if anybody else is trying to do this check out webform_mysql_views. Doesn't create a table, but a database view can be really helpful if you're doing this for integration purposes.

http://drupal.org/project/webform_mysql_views

Your Answer

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

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