1

I'm new with Drupal and I want to save a user after submitting the form, so I create a new module with 3 files: openerp_client.info, openerp_client.module and the openerp_client.install

I want to know how to save this user in my table. This is my openerp_client.install:

function openerp_client_schema() {
$schema = array();
$schema['openerp_client'] = array(
'description' => 'The base table for nodes.',
'fields' => array(
  'oc_id' => array(
    'description' => 'The primary identifier for a node.',
    'type' => 'serial',
    'unsigned' => TRUE,
    'not null' => TRUE,
  ),

  'first_name' => array(
    'description' => "User's first name.",
    'type' => 'varchar',
    'length' => 32,
    'not null' => TRUE,
    'default' => '',
  ),
  'last_name' => array(
    'description' => "User's last name.",
    'type' => 'varchar',
    'length' => 32,
    'not null' => TRUE,
    'default' => '',
  ),
   'company' => array(
    'description' => "User's company name.",
    'type' => 'varchar',
    'length' => 32,
    'not null' => TRUE,
    'default' => '',
  ),
   'email_address' => array(
    'type' => 'varchar',
    'length' => 254,
    'not null' => FALSE,
    'default' => '',
    'description' => "User's e-mail address.",
  ),
),
'indexes' => array(
  'openerp_client_oc_id' => array('oc_id'),
),

'primary key' => array('oc_id'),
);
return $schema;
}

This is what's in my implementation of hook_webform_submission_insert():

function openerp_client_webform_submission_insert($node, $submission) { 
$nid = db_insert('openerp_client')
 ->fields(array(
'first_name' => $submission->first_name,
'last_name' =>$submission->last_name, 
'company' =>$submission->company, 
'email_address' =>$submission->email_address,
))
->execute();
}

But I get this exception:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'first_name' cannot be null: INSERT INTO {openerp_client} (first_name, last_name, company, email_address) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3); Array ( [:db_insert_placeholder_0] => [:db_insert_placeholder_1] => [:db_insert_placeholder_2] => [:db_insert_placeholder_3] => ...

Why?

  • 3
    Database API. Please read that first, and come back if you have a specific question about code you've written yourself – Clive Apr 11 '14 at 9:13
  • Thanks, i get this error : PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'first_name' cannot be null: INSERT INTO {openerp_client} (first_name, last_name, company, email_address) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3); Array ( [:db_insert_placeholder_0] => [:db_insert_placeholder_1] => [:db_insert_placeholder_2] => [:db_insert_placeholder_3] => ... – Jemai Hamza Apr 11 '14 at 9:25
  • 1
    Thanks for the update @Jemai, I've edited the exception into your question too. Short answer: $submission->first_name is empty – Clive Apr 11 '14 at 9:27
  • Thanks Clive, but i insert the first_name in my form befor submitting – Jemai Hamza Apr 11 '14 at 9:30
  • 1
    Not disputing that - but by the time scope reaches your webform hook, $submission->first_name is empty. It's the only possible explanation for the PDO error you see – Clive Apr 11 '14 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.