0

I am trying to insert some datas into database table on form submit like this:

function form_example_submit($form, &$form_state) {

    if ($form_state['values']['select']=='1') $gender = 'male';
    else $gender = 'female';

    db_insert('form_example_table')->fields(array(
      'name'    => $form_state['values']['name'],
      'gender'  => $gender,
      'message' => $form_state['values']['message'],      

    ))->execute();

    drupal_set_message("Message is successfully saved"); 

    return $form;

}

My schema in .install file looks like this:

function form_example_schema() {

    $schema['form_example_table'] = array(
        'description' => 'Table for form_pokus module',
        'fields' => array(
            'id'        => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
            'name'      => array('type' => 'varchar','length' => 30,     'not null' => TRUE, 'default' => ''),
            'gender'    => array('type' => 'varchar','length' => 30,     'not null' => TRUE, 'default' => ''),
            'message'   => array('type' => 'text', 'not null' => TRUE,   'default'  => ''),
            'status'    => array('type' => 'text', 'not null' => FALSE ),
            'created'   => array('type' => 'int',  'not null' => TRUE,   'default'  => 0),          
        ),

        'primary key' => array('id'),

    );

    return $schema;
}

But nothing happens on form submit, only page is reloaded. What I am mising?

2
  • 1
    Do you see the message "Message is successfully saved"? If not, your submit function isn't running – Clive Apr 8 '15 at 9:53
  • No, but I see the message now after you indirectly suggested where error could be and after renaming 'form_example_submit' to 'form_example_form_submit'. Thanks. – Incredible Apr 8 '15 at 9:56
0

your schema

<?php
function form_example_schema() {

    $schema['form_example_table'] = array(
        'description' => 'Table for form_pokus module',
        'fields' => array(
            'id'        => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
            'name'      => array('type' => 'varchar','length' => 30,     'not null' => TRUE, 'default' => ''),
            'gender'    => array('type' => 'varchar','length' => 30,     'not null' => TRUE, 'default' => ''),
            'message'   => array('type' => 'text', 'not null' => TRUE,   'default'  => ''),
            'status'    => array('type' => 'text', 'not null' => FALSE ),
            'created'   => array('type' => 'int',  'not null' => TRUE,   'default'  => 0),          
        ),

        'primary key' => array('id'),

    );

    return $schema;
}
?>

needs to look like:

function form_example_schema() {

    $schema['form_pokus_table'] = array(
        'description' => 'Table for form_pokus module',
        'fields' => array(
            'id'        => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
            'name'      => array('type' => 'varchar','length' => 30,     'not null' => TRUE, 'default' => ''),
            'gender'    => array('type' => 'varchar','length' => 30,     'not null' => TRUE, 'default' => ''),
            'message'   => array('type' => 'text', 'not null' => TRUE,   'default'  => ''),
            'status'    => array('type' => 'text', 'not null' => FALSE ),
            'created'   => array('type' => 'int',  'not null' => TRUE,   'default'  => 0),          
        ),

        'primary key' => array('id'),

    );

    return $schema;
}

you had the wrong table name as the schema index.

3
  • I have 'pokus' word everywhere instead of 'example' , but I renamed it here on stackexchange because it is foreign language word and forgot to rename this one. Problem must be somewhere else. – Incredible Apr 8 '15 at 9:47
  • OP is using db_insert('form_example_table'), so the table name is already correct – Clive Apr 8 '15 at 9:52
  • oh there was a previous revision, my bad – Clive Apr 8 '15 at 9:52
0

The problem was that the proper hook name is not form_example_submit but form_example_form_submit.

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.