Here is a code to insert data from the form to the table in a database in Drupal 8

BY Best Interview Question ON 15 Apr 2020

Example

public function submitForm(array &$form, FormStateInterface $form_state) {
$field = $form_state->getValues();
$name = $field['name'];
$email = $field['email'];
$field_arr = [
   'name' => $name,
   'email' => $email
];
$query = \Drupal::database();
$query->insert('users')
   ->fields($field_arr)
   ->execute();
drupal_set_message("data successfully saved");
}