Community

insert data from form with submit into table of database

Hi
Could you please tell me how I insert data from form (built with Form API) with submit into table of database?
mysql is used

Thanks

Comments

http://drupal.org/node/107436

Jaypan We build websites

See the following example of

See the following example of my simple custom form for validating the data and inserting into database.

<?php
/**
* Implements security_settings_form().
*/
function security_settings_form($form, &$form_state) { 

 
$form['privacy_settings'] = array(
     
'#type' => 'radios',    
     
'#options' =>  array('public' => 'Public', 'private' => 'Private', 'other' => 'Use individual Page settings'),  
     
'#validated' => TRUE,
  );
 
$form['passwd'] = array(
     
'#type' => 'textfield',
     
'#title' => 'Password',   
     
'#maxlength' => 15,
     
'#size' => 15,     
  );

 
$form['save'] = array(
     
'#type' => 'submit',
     
'#value' =>  'Save',
     
'#submit' =>  array('security_settings_form_submit'),
     
'#validate' =>  array('security_settings_form_validate'),
  );
  return
$form;
}

/**
* Implements security_settings_form_validate().
*/
function security_settings_form_validate($form, $form_state) {
  if (
$form_state['values']['privacy_settings'] == 'private') {
    if ((
strlen($form_state['values']['passwd']) < 6)) {
     
form_set_error('passwd', 'Please enter minimum six charecters of password');
    }
  }
}

/**
* Implements security_settings_form_submit().
*/
function security_settings_form_submit($form, $form_state) {
  global
$user;
 
// Here u can insert Your custom form values into your custom table.
 
db_insert('security_settings')
    ->
fields(array(
     
'uid' => $user->uid,
     
'passwd' => $form_state['values']['passwd'],      
    ))->
execute();
   
drupal_set_message("successfully saved Security Settings");
}
 
?>
nobody click here