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

jaypan’s picture

Contact me to contract me for D7 -> D10/11 migrations.

maheshg85’s picture

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

/**
 * 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"); 
}
  
debasisnaskar’s picture

Hi, I am very new to Drupal 8, And I am trying to insert input field data into DB, But its not happening. I want to know the very last method for Drupal 8 to save my input field data into Database. Thanks in Advance!

function security_settings_form_submit($form, $form_state)
sunil singh’s picture

Hi @ debasisnaskar

You can use below code to insert input field data into database table. in below code, i have used 'table_name' for Db table and 'name' form field name, replace your  custom table name and form field name here.

  public function submitForm(array &$form, FormStateInterface $form_state) {
   $field = $form_state->getValues();
    $name = $field['name'];
    $field_arr = [
        'name' => $name,

    ];
    $query = \Drupal::database();
    $query->insert('table_name')
            ->fields($field_arr)
            ->execute();
    drupal_set_message("data successfully saved");
  }

s_dinda’s picture

Is it require to give form actions to get relevant output.
sorry i'm fully new to Drupal.

Anonymous’s picture

Dear "mahesh85", about your code posted 3 years ago ...
("See the following example of my simple custom form for validating the data and inserting into database.")

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");
}

After many days looking for a solution to save my form to my database,
your little piece of code ...
SAVED ME !

You were the only person who explained straight the solution !

Kind regards from Belgium
09.03.2016

Azor Ahai’s picture

FYI, that information is included in the Creating Custom Modules Tutorial. There is a lot of good stuff in those tutorials if you need any further help.

imunklovarian’s picture

i go to the link and not found the solution how to make custom form to insert database? not at all it just documentation how to make a custom module page, menu block etc....:)