Form data not being entered into the SQL statement I have created a form which is not entering the data I enter in the fields into the sql statement. I just got the error: 


  user warning: Duplicate entry '' for key 'PRIMARY' query: INSERT INTO subscription_info (first, last, email, phone) VALUES ('','','','') in C:\EasyPHP-5.3.8.1\modules\drupal620x120318224310\sites\all\modules\email_form\email_form.module on line 124.


Here is the code:

function email_form_my_form($form_state) {
  $form['info'] = array(
    '#type' =&gt; 'fieldset',
    '#title' =&gt; t('Personal Information'),
    '#collapsible' =&gt; TRUE,
    '#collapsed' =&gt; FALSE,
  );

  $form['info']['first'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('First name'),
    //'#default_value' =&gt; $form_state['values']['first'], // changed
    '#description' =&gt; "Please enter your first name.",
    '#size' =&gt; 20,
    '#maxlength' =&gt; 20,
   );

   $form['info']['last'] = array(
     '#type' =&gt; 'textfield',
     '#title' =&gt; t('Last name'),
     '#size' =&gt; 20,
     '#maxlength' =&gt; 20
     //'#default_value' =&gt; $form_state['values']['last'], // added
   );

   $form['info']['email'] = array(
     '#type' =&gt; 'textfield',
     '#title' =&gt; t('Email address'),
     '#size' =&gt; 30
   );

  $form['info']['phone'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Phone Number'),
    '#description' =&gt; "Format: 555-555-5555",
    '#size' =&gt; 10,
    '#maxsize' =&gt; 10
  );

  $form['subscription'] = array(
    '#type' =&gt; 'fieldset',
    '#title' =&gt; t('Subscription Information'),
    '#collapsible' =&gt; TRUE,
    '#collapsed' =&gt; FALSE
  );

  $form['subscription']['content'] = array(
    '#type' =&gt; 'checkboxes',
    '#title' =&gt; t('Events'),
    '#description' =&gt; t('Please chose which events you would like to follow'),
    '#options' =&gt; array(
      'Baseball' =&gt; t('Baseball'),
      'Basketball' =&gt; t('Basketball'),
    ),
  );                    

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

  $form['clear'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; 'Reset form',
    '#validate' =&gt; array('email_form_my_form_clear'),
  );

  return $form;
}

function email_form_my_form_submit($form, &amp;$form_state) {
  //unset($form_state['storage']);
if (db_query("INSERT INTO {subscription_info} (first, last, email, phone) VALUES ('%s','%s','%s','%s')", $form_state['values']['info']['first'], $form_state['values']['info']['last'], $form_state['values']['info']['email'], $form_state['values']['info']['phone'])) {
      //$form_state['values']['subscription']['content'] use this later...
      drupal_set_message(t('The form has been submitted.'));
}
else {
      drupal_set_message(t('The form had an error in it.'));
    }
  }
}
Drupal flattens the form values array so the element set at $form['info']['first'] will be available in $form_state['values']['first']. 

If you want it to be available in $form_state['values']['info']['first'] you need to set the #tree attribute on the parent element:

$form['info'] = array(
  '#type' =&gt; 'fieldset',
  '#title' =&gt; t('Personal Information'),
  '#collapsible' =&gt; TRUE,
  '#collapsed' =&gt; FALSE,
  '#tree' =&gt; TRUE
);


There's more info available on the #tree and #parents documentation page if you're interested.

The alternative is to change your db_query() call to use the "default" method:

db_query("INSERT INTO {subscription_info} (first, last, email, phone) VALUES ('%s','%s','%s','%s')", 
  $form_state['values']['first'], 
  $form_state['values']['last'],  
  $form_state['values']['email'],
  $form_state['values']['phone']
);