Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By MiguelD on
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
http://drupal.org/node/1074360
See the following example of
See the following example of my simple custom form for validating the data and inserting into database.
Great! But Will it work for Drupal 8??
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!
Database Insert example.
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");
}
submitting to comments
Is it require to give form actions to get relevant output.
sorry i'm fully new to Drupal.
Your little piece of code ... SAVED ME !
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
FYI, that information is
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.
where the specific explanation for the sollution?
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....:)