How to push form values to database in Drupal I am building a drupal site in which I would like the admin to have the ability to input goals into a table on the drupal database that can then be output to other users to be completed.

The form for the input and the table get created, however the values are not added into the table when submit is pressed and I'm not sure why.

I have the following code from the module that I am hooking into my site. I think the problem occurs at the top where I am creating the fields, or at the bottom when I submit the values. I'm really not sure.

Can anyone tell me where I went wrong and how to fix it?


function achievementList_init() {
if(!(db_table_exists('achievements'))) {
    $achvmnt_list_schema = array(
        'description' =&gt; 'Achievement Fields',
        'fields' =&gt; array (
            'achvmntID' =&gt; array('type' =&gt; 'int', 'unsigned' =&gt; TRUE, 'AUTO_INCREMENT' =&gt; TRUE, 'not null' =&gt; TRUE),

            'achvmntName' =&gt; array('type' =&gt; 'varchar', 'length' =&gt; 256, 'not null' =&gt; TRUE, 'default' =&gt; ''),

            'achvmntDesc' =&gt; array('type' =&gt; 'varchar', 'length' =&gt; 256, 'not null' =&gt; TRUE, 'default' =&gt; ''),

            'achvmntPts' =&gt; array('type' =&gt; 'int', 'unsigned' =&gt; TRUE, 'not null' =&gt; TRUE),

    ));
    db_create_table('achievements', $achvmnt_list_schema);
}
}

function achievementList_menu($form, &amp;$form_state) {
$items = array();
$items['achievementList/addAchievement'] = array(
    'title' =&gt; 'Achievement Input',
    'description' =&gt; 'Input Achievement info to add achievement to database',
    'page callback' =&gt;'drupal_get_form',
    'page arguments' =&gt; array('achievementList_form'),
    'access callback' =&gt; TRUE
);
return $items;
}

function achievementList_form($form, &amp;$form_state) {
$form['name'] = array (
    '#type' =&gt; 'textfield',
    '#title' =&gt; 'Achievement Name',
    '#size' =&gt; '20',
    '#maxlength' =&gt; '20',
    '#required' =&gt; TRUE
);
$form['description'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; 'Achievement Description',
    '#size' =&gt; 50,
    '#maxLength' =&gt; 400,
    '#required' =&gt; TRUE

);
$form['points'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; 'Point Award',
    '#size' =&gt; '20',
    '#maxlength' =&gt; '20',
    '#required' =&gt; TRUE
);
$form['add_button'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Add Achievement')
);
return $form;
    }

function achievementList_validate($form, &amp;$form_state) {
$name = $form_state['values']['name'];

if($form_state['values']['name'] = '') {
    form_set_error('name', t('Achievement must have a name!'));
}
if($form_state['values']['description'] = '') {
    form_set_error('name', t('Please describe the achievement!'));
}
if(!($form_state['values']['points'] &gt; 0)) {
    form_set_error('name', t('Achievement must be worth points!'));
}
}

function achievementList_submit($form, &amp;$form_state) {
db_insert('achievements')
-&gt;fields(array(
'achvmntName' =&gt; $form_state['values']['name'],
'achvmntDesc' =&gt; $form_state['values']['description'],
'achvmntPts' =&gt; $form_state['values']['points'],
))
-&gt;execute();
}
You're not far off, a couple of things:


Creating a table like that in an init hook is definitely not recommended; Drupal provides hook_schema() so you can describe tables to the system. Uninstall and install your module again after implementing it and you'll see the table appear.
The reason your insert is failing is because you have a NOT NULL integer field which you're not providing (achvmntID). Drupal has the serial field type rather than an AUTO_INCREMENT key to get a serial field:

'achvmntID' =&gt; array(
  'type' =&gt; 'serial', 
  'unsigned' =&gt; TRUE,
  'not null' =&gt; TRUE,
),