0

I'm unable to save the details into database after the form is submitted in Drupal 7.

Below is the code for my custom form to save the details to DB,

function form_module_menu() {
  $items = array();
  $items['form_module/form'] = array(
    'title' => t('Registration form'),
    'page callback' => 'form_module_form',
    'access arguments' => array('access content'),
    'description' => t('Registration Form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function form_module_form() {
  // This form calls the form builder function via the
  // drupal_get_form() function which takes the name of this form builder
  // function as an argument. It returns the results to display the form.
  return drupal_get_form('form_module_my_form');
}

function form_module_my_form($form_state) {
  $form = array();
    // This is the first form element. It's a textfield with a label, "fName"
  $form['fName'] = array(
        '#type'      => 'textfield',
        '#title'     => t('First Name'),
        '#required'  => TRUE,
        '#size'      => 60,
        '#maxlength' => 64,
  );
  // This is the lastname form element. It's a textfield with a label, "lName"
  $form['lName'] = array(
        '#type'     => 'textfield',
        '#title'    => t('Last Name'),
  );
  // This is the email address form element. It's a textfield with a label, "email"
  $form['email'] = array(
        '#type'     => 'textfield',
        '#title'    => t('Email'),
        '#required' => TRUE, //must fill this if set to TRUE
        '#rules'    => array(
                            ),
        '#filters'  => array('trim', 'uppercase')
  );
  // This is the password and confirm password form element. It's a textfield with a label, "cpwd"
  $form['cpwd'] = array(
        '#type'     => 'password_confirm',
  );
  $form['submit_button'] = array(
        '#type'     => 'submit',
        '#value'    => t('Submit'),
  );
  $form['clear'] = array(
        '#type'     => 'submit',
        '#value'    => 'Reset form',
        '#validate' => array('my_module_my_form_clear'),
  );
  //print l('return to main home page', '<front>');
  return $form;
}

//Rebuild the form
function my_module_my_form_clear($form, &$form_state) {
    $form_state['rebuild'] = TRUE;
}

function form_module_email_validate($element, &$form_state, $form) {
  //email validation not working how to validate email
  $mail = $form_state['values']['email'];
  if (strpos($mail,'@') !== true) {
    form_error($element, t('Please enter a valid email address.'));
  }
  else{
    form_error($element, t('Please enter a valid email address.'));
  }
}

function form_module_my_form_submit($form, &$form_state) {
    drupal_set_message($form_state['values']['fName'].$form_state['values']['lName'].$form_state['values']['email']);
    $sql = "INSERT INTO {customers} (first_name,last_name,email_addr,password) VALUES('%s', '%s', '%s' , '%s')";
    db_query($sql,
        array('%s' => $form_state['values']['fName']),
        array('%s' => $form_state['values']['lName']),
        array('%s' => $form_state['values']['email']),
        array('%s' => $form_state['values']['cpwd'])
    );
    drupal_set_message(t('Thanks for registering with us. You will receive an email shortly to activate your account.'));
    //after successfully saved how to send mail to user with his login details
}

The submit handler is called and the values are displayed.but in database all rows are filled with '%s'. I new to working in Drupal. How to store passwords and how to save the data and send an email to the user registering..

2

As stated by @Molot this should not be used generally. Recommended methods would be db_insert and db_merge.

Placeholders are layed wrong, they need to be in single array. Use

 $sql = "INSERT INTO {customers} (first_name,last_name,email_addr,password)
         VALUES(:fName, :lName, :email , :cpwd)";
db_query($sql,
    array(
      ':fName' => $form_state['values']['fName'],
      ':lName' => $form_state['values']['lName'],
      ':email' => $form_state['values']['email'],
      ':cpwd' => $form_state['values']['cpwd'],
   )
);
| improve this answer | |
  • this is also saving :fName for firstname and :lName for lastname and so on... – Lucky Nov 26 '13 at 7:35
  • Updated the answer by dropping the quotes on placeholders. Db api should take care of placing them automatically. – user20301 Nov 26 '13 at 7:39
  • 2
    Note: it should be discouraged to use db_query for inserts. db_insert and db_merge are much safer, and their syntax makes it harder to make mistakes. Can't upvote without this in answer. – Mołot Nov 26 '13 at 7:43
  • @user20301 thnks for the answer.EDIT: i removed the comma after the array closing bracket.. Now the data is saving.. – Lucky Nov 26 '13 at 8:18
  • 1
    Validate E-mail Addresses drupal.org/node/279127, Sending mail api.drupal.org/api/drupal/includes!mail.inc/function/…. – user20301 Nov 26 '13 at 8:42
0

As stated by @Molot and @user20301 using the recommended method db_insert i have created this method for saving the values to db,

$values = array(
    'fname' => $form_state['values']['fName'],
    'lname' => $form_state['values']['lName'],
    'email' => $form_state['values']['email'],
    'password' => $form_state['values']['cpwd']
);
$insert = db_insert('customers')
    -> fields(array(
    'first_name' => $values['fname'],
    'last_name' => $values['lname'],
    'email_addr' => $values['email'],
    'password' => md5($values['password']),
))
->execute();

this works fine for doing the insert operation..thanks..

| improve this answer | |

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.