0
function lawyer_register_form($form,&$form_submit) 
{
$form = array();
$form['name'] = array(
    '#title' => 'Username',
    '#description' => 'choose a username',
    '#type' => 'textfield',
    '#required' => TRUE,
);

$form['mail'] = array(
    '#title' => 'email',
    '#description' => 'enter a valid email address',
    '#type' => 'textfield',
    '#required' => TRUE,
);

 $form['fname'] = array(
 '#title' => t('Full Name'),
 '#type' => 'textfield',
 '#size' => 20,
 '#maxlength' => 20,
);

 $form['phone_no'] = array(
 '#title' => t('phone_no'),
 '#type' => 'textfield',
 '#description' => t(''),
 '#size' => 20,
 '#maxlength' => 20,
);

$form['street_address'] = array(
'#title' => t('Street Address'),
'#type' => 'textfield',
'#size' => 50,
'#maxlength' => 100,
);        



$form['county'] = array(
'#title' => t('County'),
'#type' => 'textfield',
'#autocomplete_path' => 'lawyer_register-autocomplete-engine',
'#size' => 20,
'#maxlength' => 20,
 );       

$form['send_message'] = array(
    '#type' => 'checkbox',
    '#title' => t('Notify user of new account via email.'),
    '#description' => 'If left unchecked, a message will not be sent.',
    '#default_value' => 1,
 );

 $form['submit'] = array(
  '#value' => 'Submit',
 '#type' => 'submit'
 ); 
 return $form;
 }


 function lawyer_register_form_submit($form, &$form_submit)
 {
 $new_password = user_password();
 $edit = array(
  'name' => $form_submit['values']['name'],
  'pass' => $new_password,
  'mail' => $form_submit['values']['mail'],
  'init' => $form_submit['values']['mail'],
  'status' => 1,
  'access' => REQUEST_TIME,
  ');
   $edita=$edit->uid;
  db_insert('field_data_field_name')
  ->fields(array(
'entity_type' => 'user',
'bundle' => 'user',
'deleted' => 0,
'entity_id' => $edita,
'language' => 'und',
'delta' => 0,
'field_name_value' =>$form_submit['values']['fname'],
))->execute();
   db_insert('field_data_field_county')
->fields(array(
'entity_type' => 'user',
'bundle' => 'user',
'deleted' => 0,
'entity_id' => $edita,
'language' => 'und',
'delta' => 0,
'field_county_value' =>$form_submit['values']['county'],
))
->execute();
    db_insert('field_data_field_address')
->fields(array(
'entity_type' => 'user',
'bundle' => 'user',
'deleted' => 0,
'entity_id' => $edita,
'language' => 'und',
'delta' => 0,
'field_address_value' =>$form_submit['values']['street_address'],
))->execute();
    db_insert('field_data_field_phone_no')
->fields(array(
'entity_type' => 'user',
'bundle' => 'user',
'deleted' => 0,
'entity_id' => $edita,
'language' => 'und',
'delta' => 0,
'field_userphone_value' =>$form_submit['values']['phone_no'],
))->execute();

   user_save(drupal_anonymous_user(), $edit);

I have already created table in database. I want to insert value of

fname, street_address, phone_no & county

In those table. How is it possible?

I have searched lots of time in google but not getting right answer.

  • actually i created custom user registration login form. now i need to add fname, street_address, phone_no & county in to the database.so please help me how i can do. – Swapnil0226 Oct 18 '14 at 4:25
1

if you wants to simple insert the field values in a different table you can write insert query in form submit.

 function lawyer_register_form($form,&$form_state) 
{
$form = array();
$form['name'] = array(
    '#title' => 'Username',
    '#description' => 'choose a username',
    '#type' => 'textfield',
    '#required' => TRUE,
);

$form['mail'] = array(
    '#title' => 'email',
    '#description' => 'enter a valid email address',
    '#type' => 'textfield',
    '#required' => TRUE,
);

 $form['fname'] = array(
 '#title' => t('Full Name'),
 '#type' => 'textfield',
 '#size' => 20,
 '#maxlength' => 20,
);

 $form['phone_no'] = array(
 '#title' => t('phone_no'),
 '#type' => 'textfield',
 '#description' => t(''),
 '#size' => 20,
 '#maxlength' => 20,
);

$form['street_address'] = array(
'#title' => t('Street Address'),
'#type' => 'textfield',
'#size' => 50,
'#maxlength' => 100,
);        



$form['county'] = array(
'#title' => t('County'),
'#type' => 'textfield',
'#autocomplete_path' => 'lawyer_register-autocomplete-engine',
'#size' => 20,
'#maxlength' => 20,
 );       

$form['send_message'] = array(
    '#type' => 'checkbox',
    '#title' => t('Notify user of new account via email.'),
    '#description' => 'If left unchecked, a message will not be sent.',
    '#default_value' => 1,
 );

 $form['submit'] = array(
  '#value' => 'Submit',
 '#type' => 'submit'
 ); 
 return $form;
 }



    function lawyer_register_form_submit($form, &$form_state)
         {
         $username = $form_state['values']['name'];
         $new_password = user_password();
         $edit = array(
          'name' =>  $form_state['values']['name'],
          'pass' => $new_password,
          'mail' => $form_state['values']['mail'],
          'init' =>  $form_state['values']['mail'],
          'status' => 1,
          'access' => REQUEST_TIME,
          'field_first_fname' => array(LANGUAGE_NONE => array(array('value' =>     $form_state['values']['fname']))),
          'field_first_street_address' => array(LANGUAGE_NONE => array(array('value' =>  $form_state['values']['street_address']))),
          'field_first_phone_no' => array(LANGUAGE_NONE => array(array('value' =>  $form_state['values']['phone_no']))),
          'field_first_county' => array(LANGUAGE_NONE => array(array('value' =>  $form_state['values']['county']))),
          );

    user_save(NULL, $edit);
$user_id = db_query("SELECT uid FROM {users} WHERE name = :username", array(':username' => $username))->fetchField();

        $insert = db_insert('table_name') // in which table you want to insert
                  -> fields(array(
                        'uid' => $user_id,
                        'fname' => $form_state['values']['fname'],
                        'country' => $form_state['values']['country'],
                        'phone_no' => $form_state['values']['phone_no'],
                       //write other fields you want to save
                        ))->execute();
                }
  • all the fields are in diffrent table. – Swapnil0226 Oct 17 '14 at 8:29
  • you wants to insert all the fields value in a single table right? – DRUPWAY Oct 17 '14 at 8:32
  • No, actually we have created custom user registration login form and insert data in users table. so all other remaining information like fname, phone, street address and county i want to store in 4 different's table – Swapnil0226 Oct 17 '14 at 8:35
  • ok, by using the above query you can save the rest data in a separate table but it is better to create the field here "admin/config/people/accounts/fields" – DRUPWAY Oct 17 '14 at 8:43
  • first of all you need to clear about how you want to save the rest field information ? 1.You can create a separate table and save all information in a single table. 2. Create fields "admin/config/people/accounts/fields" and user_save() will save in different table no need to create separate field. – DRUPWAY Oct 17 '14 at 8:53
0
 $user = user_load(1); 
    or
 $user = user_load(array('uid' => '1'));
echo '<pre>';
print_r($user);
die();

check the fields then save

i think this much code in enough to save fields.

function lawyer_register_form_submit($form, &$form_state)
             {
             $username = $form_state['values']['name'];
             $new_password = user_password();
             $edit = array(
              'name' => $form_state['values']['name'],
              'pass' => $new_password,
              'mail' => $form_state['values']['mail'],
              'init' => $form_state['values']['mail'],
              'status' => 1,
              'access' => REQUEST_TIME,
              'field_first_fname' => array(LANGUAGE_NONE => array(array('value' =>    $form_state['values']['fname']))),
              'field_first_street_address' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['street_address']))),
              'field_first_phone_no' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['phone_no']))),
              'field_first_county' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['county']))),
              );

        user_save(NULL, $edit);
    }

Your Answer

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

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