3

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' => 'Achievement Fields',
        'fields' => array (
            'achvmntID' => array('type' => 'int', 'unsigned' => TRUE, 'AUTO_INCREMENT' => TRUE, 'not null' => TRUE),

            'achvmntName' => array('type' => 'varchar', 'length' => 256, 'not null' => TRUE, 'default' => ''),

            'achvmntDesc' => array('type' => 'varchar', 'length' => 256, 'not null' => TRUE, 'default' => ''),

            'achvmntPts' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),

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

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

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

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

function achievementList_validate($form, &$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'] > 0)) {
    form_set_error('name', t('Achievement must be worth points!'));
}
}

function achievementList_submit($form, &$form_state) {
db_insert('achievements')
->fields(array(
'achvmntName' => $form_state['values']['name'],
'achvmntDesc' => $form_state['values']['description'],
'achvmntPts' => $form_state['values']['points'],
))
->execute();
}
| improve this question | | | | |
3

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' => array(
      'type' => 'serial', 
      'unsigned' => TRUE,
      'not null' => TRUE,
    ),
    
| improve this answer | | | | |
1

You need to make some changes like

It is better to create hook_schema as @Clive mention above.

For this recommended to create achievementList.install file in module

 <?php
/**
 * Implementation of hook_install().
 */
function achievementList_install() {
    //drupal_install_schema('achievementList');
    //for d6 uncomment above line
}

/**
 * Implementation of hook_uninstall().
 */
function achievementList_uninstall() {
    //drupal_uninstall_schema('achievementList');
    //for d6 uncomment above line
}

function achievementList_schema() {
    $schema['achievements'] = array(
    'description' => t('Achievement Fields'),
        'fields' => array(
                'achvmntID' => array(
                    'type' => 'serial',
                    'unsigned' => TRUE,
                    'not null' => TRUE
                ),
                'achvmntName' => array(
                    'type' => 'varchar',
                    'length' => '256',
                    'not null' => TRUE,
                    'default' => ''
                ),
                'achvmntDesc' => array(
                    'type' => 'varchar',
                    'length' => '256',
                    'not null' => TRUE,
                    'default' => ''
                ),
                'achvmntPts' => array(
                    'type' => 'int',  
                    'not null' => TRUE, 
                    'size' => 'normal',
                    'auto increment' => TRUE,
                ),
            ),
            'primary key' => array('achvmntID'),
        );
    return $schema;
}

function achievementList_menu($form, &$form_state) does not contain parameter.

Use function achievementList_menu()

function achievementList_validate($form, &$form_state) here "form" missing

use function achievementList_form_validate()

function achievementList_submit($form, &$form_state) here "form" missing

use function achievementList_form_form()

.module file

<?php
function achievementList_menu() {
$items = array();
$items['achievementList/addAchievement'] = array(
    'title' => 'Achievement Input',
    'description' => 'Input Achievement info to add achievement to database',
    'page callback' =>'drupal_get_form',
    'page arguments' => array('achievementList_form'),
    'access callback' => TRUE
);
return $items;
}

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

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

function achievementList_form_validate($form, &$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'] > 0)) {
    form_set_error('name', t('Achievement must be worth points!'));
}
}

function achievementList_form_submit($form, &$form_state) {
//print_r($form_state);
//die();
db_insert('achievements')
->fields(array(
'achvmntName' => $form_state['values']['name'],
'achvmntDesc' => $form_state['values']['description'],
'achvmntPts' => $form_state['values']['points'],
))
->execute();
}
| improve this answer | | | | |
  • @cluckles are you able to fix the above issue? – DRUPWAY Oct 16 '14 at 19:54
  • Sort of. I get the table and everything going but it still won't submit. It looks like the submit function is not successfully being called. I cant echo or print_r anything out of it – jrounsav Oct 17 '14 at 18:48
  • Can you update your question what you have changed as i have mentioned in submit function you missed "form" word. – DRUPWAY Oct 18 '14 at 1:51
  • @Cluckles did you manage to get this work? I'm intrested of this kind of functionality too. – hene Jan 18 '15 at 16:05
  • @hene Yes I did! It has been a while, but I think the issue was that it was recreating the table every time the page was refreshed(possibly because of the init function holding the raw create table functionality). Now I have an if statement in the init. If the table does not exist, create it. Now everything populates properly per the code above, with a couple tweaks to the UI! – jrounsav Jan 22 '15 at 17:42

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.