I'm getting an error and this is really the first time I've tried to actively interact with the database from a module. Just hoping that someone can spot the problem and help me correct it. Basically it takes a date value from the form (Date module is used to get the date formatting). I want it to save into a table with three fields

wedid/primary/auto incremental
uid/ user uid
field_weddingdate / the date entered into the form + 3 months (90days)

Schema works as expected. The form works as expected but I need to change the default value to read from the database and not use variable_set. The form_submit gives the following error.

error : Recoverable fatal error: Argument 2 passed to db_query() must be an array, string given,

Currently I have the following code for hook_schema , hook_form , hook_form_submit

<?php
/*
 * Schema
 */
function weddingdate_schema(){
  $schema['wedding'] = array(
  'description' => 'For storing information related to wedding',
  'fields' => array(
    'wedid' => array(
      'description' => 'Wedding table id',
      'type' => 'serial',
      'not null' => TRUE,
    ),
    'uid' => array(
      'description' => 'Copy of user id our link to the user',
      'type' => 'int',
      'not null' => FALSE,
    ),
    'field_weddingdate' => array(
      'description' => 'The calculated Wedding Date',
      'type' => 'int',
      'not null' => FALSE,
    ),
  ),
  'primary key' => array('wedid'),
);
  return $schema;
}
?>

the following form and form submit hooks.

<?php
function weddingdate_form($form, &$form_state) {
// Text field for the e-mail subject.
  $format = 'Y:F:j';

$form['weddingdate_last_cycle'] = array(
'#type' => 'date_select',
'#title' => t('The last Cycle.'),
'#description' => t('The last time you had a wedding cycle.'),
    '#date_format' => $format,
'#default_value' => variable_get('weddingdate_last_cycle',
'0000-00-00 0:0:0'),
'#required' => FALSE,
);
// Submit button
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings'),
);
return $form;
}


/**
* Save configuration settings for Wedding Cycle module.
*/
function weddingdate_form_submit($node, &$form_state) {
  global $user;
  $userid = $user->uid;
  $formdate = variable_get('weddingdate_last_cycle');
  $timeperiod = 90*86400;
  $formdatestore = $formdate + $timeperiod;

  $sql = "insert into {weddingdate} (uid,field_weddingdate) values ($userid,$formdatestore)";
    $db_result = db_query($sql, $form_state['values']['weddingdate_last_cycle']);
    if ($db_result) {
        drupal_set_message(
         "Wedding date cycle".$form_state['values']['weddingdate_last_cycle'].
         " has been successfully added to the database"
         );
    } else {
        drupal_set_message(
           "An unexpected error has occurred. DB".
           $form_state['values']['weddingdate_last_cycle']." has not been created");
    }
}

?>

Sorry for the long post just want to get plenty of info into it so I can find a solution.

Cheers
Vmpwraith

Comments

lorinpda’s picture

Hi,
If understand your post, you just want to clear the database exception. For inserts in Drupal 7 you should use db_insert() http://api.drupal.org/api/drupal/includes--database--database.inc/functi... .

In addition the method signature on db_query changed from Drupal 6 to Drupal 7. In Drupal 7 you change your placholders to :myplaceholder and then you pass all the values in a keyed array array(':myplaceholder' => VALUE, .....)

But again, you need to do an insert, therefore use db_insert (see the docs for Drupal 7 db_query, only for selects) http://api.drupal.org/api/drupal/includes--database--database.inc/functi...

Hope that helps.

JasonMoreland’s picture

Changed hook_form_submit. It now inserts into the database fine. The problem is that I'm not capturing the formdate element from the Form. So the date is not calculated at all. Any chance you could share how to capture that form element for use?

I guess I'll have to add a db_update / db_insert if, else statement later as I only want to have one entry in the database per user.

<?php
function weddingdate_form_submit($node, &$form_state) {
  global $user;
  $userid = $user->uid;
  $formdate = variable_get('weddingdate_last_cycle');
  $timeperiod = 90*86400;
  $formdatestore = $formdate + $timeperiod;

  $insertquery = db_insert('wedding') // Table name no longer needs {}
->fields(array(
  'uid' => $userid,
  'field_weddingdate' => $formdatestore,
))
->execute();
}
?>

cheers
Vmpwraith

JasonMoreland’s picture

Ok getting better and better :-)
Now I have narrowed the problem to the way the data is stored in the database. I seem to have a problem in the formating of the data from the DATE module on the form and the field_weddingcycle the function hook_form_submit and the database. I have set up a DATE form type of date_select to use the formating of $format = 'Y:m:j:0:0:0'; but my calculation needs a timestamp I believe and the database is using an int type.

Has anyone else got any ideas on a simple way to add the date in and out of the database?

<?php
function weddingcycle_form_submit($node, &$form_state) {
  global $user;
  $userid = $user->uid;
  $formdate = $form_state['values']['weddingcycle_last_cycle'];
  $timeperiod = 90*86400;
  $formdatestore = $formdate + $timeperiod;

  $insertquery = db_insert('weddingcycle') // Table name no longer needs {}
->fields(array(
  'uid' => $userid,
  'field_weddingcycle' => $formdatestore,
))
->execute();
}
?>
lorinpda’s picture

Hi,
I have a post my site that should help. In my post I am using the Drupal 7 Field API to define a custom date field. The date field is persisted in the database as an int.

I've included a few helper functions which converts Drupal's Form API 'date' element type to an int. I also have a help function that converts the int back in to something that can be rendered by the date widget.

You'll need to extend or change to adopt to your specifics. However, I think it should save some time. Feel free to check it out here:
http://public-action.org/content/drupal-7-field-api-drupal-7-field-api-s...

JasonMoreland’s picture

Managed to solve it using this.. converting the $form_state value with $calcdate = strtotime($formdate);
Allows me to use the nice drop downs created by the DATE module.

A field may be a better way to do it though long term. Its been a struggle but I've already learnt a lot more about drupal and php :-)

<?php
function wedding_date_form_submit($node, &$form_state) {
  global $user;
  $userid = $user->uid;
  $formdate = $form_state['values']['wedding_date_last_cycle'];
  $calcdate = strtotime($formdate);
  $timeperiod = 90*86400;
  $formdatestore = $calcdate + $timeperiod;

  $insertquery = db_insert('wedding_date') // Table name no longer needs {}
->fields(array(
  'uid' => $userid,
  'field_wedding_date' => $formdatestore,
))
->execute();

}

?>

Cheers for taking the time to help out lorinpda
Vmpwraith

n3_nash’s picture

Drupal 7:

function intelligentsystem_menu(){
....
}
function intelligent_system_home_output(){
header('Content-type: text/plain; charset=UTF-8');
header('Content-Disposition: inline');
$content = intelligent_system_home_nameform();
return $content;

}

function intelligent_system_home_nameform(){

$form['title'] = array(
'#type' => 'fieldset',
'#title' => t('Enter the information :'),
);

$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name :'),
);

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);

$form['#submit'] = 'intelligent_system_home_nameform_submit';
return $form;
}

function intelligent_system_home_nameform_submit($form, &$form_state) {
drupal_set_message(t('The data is being Analyzed!!'));
}

I have tried calling a user defined function also rather than the above mentioned _submit..but nothing is working..drupal 6 used to support the above...but drupal 7 doesnt seem to..have checked out many forums..but there seems to be no solution..pls help..