0

Iam new to drupal.I have created a custom module named firstmodule .I have created a form using the tutorials section of drupal 7 when is submit i want to store this details in the registration which is present in db.

firstmodule.module

function firstmodule_form1_submit($form, &$form_state)

{

$firstname = $form_state['values']['name'];
    $username = $form_state['values']['username'];
    $password = $form_state['values']['password'];
    $year_of_birth = $form_state['values']['year_of_birth'];
    $email = $form_state['values']['email'];
    $file = $form_state['values']['file'];

I am using the insert query in the following way:

        db_insert('registration')->fields(array(
          'firstname' => $firstname,
          'username' => $username,
          'password' => $password,
          'yearofbirth' =>$year_of_birth,
          'email' => $email,
          'file' => $file,))
          ->execute();
}

it is showing me the error

Recoverable fatal error: Object of class stdClass could not be converted to string in DatabaseStatementBase->execute() (line 2168 of /var/www/drupal-7.22/includes/database/database.inc).

please help with it

3
  • there is not enough information to help you, are you trying to upload files? Commented Jun 12, 2013 at 5:10
  • ya my uploading is done and my files are even moving to a folder files in drupal.the problem is after successful validation and clicking on submit button iam not able to store the values in the database.it throws the above error.
    – bangari
    Commented Jun 12, 2013 at 5:36
  • I have solved my issue everything is saving in database but the uploaded file path is not saving i used $form_state['values']['file'] to store path in db but not storing
    – bangari
    Commented Jun 12, 2013 at 5:49

1 Answer 1

2

Here is an example of a file upload with some validation, hope it helps

function file_upload_form_validate($form, &$form_state) {

  $file = file_save_upload('file', array(
    'file_validate_extensions' => array('txt csv'), 
    'file_validate_size' => array(10000000),
  ));
  // If the file passed validation:
  if ($file) {
    // Move the file, into the Drupal file system
    if ($file = file_move($file, 'public://')) {
      // Save the file for use in the submit handler.
      $form_state['storage']['file'] = $file;
    }
    else {
      form_set_error('file', 'there was an error uploading the file');
    }
  }
  else {
    form_set_error('file', 'No file uploaded');
  }
}

function file_upload_form_submit($form, &$form_state) {
  $file = $form_state['storage']['file'];
  // We are done with the file, remove it from storage.
  unset($form_state['storage']['file']);
  // Make the storage of the file permanent
  $file->status = FILE_STATUS_PERMANENT;
  // Save file status.
  file_save($file);
  // here you can save your data
  $firstname = $form_state['values']['name'];
  $username = $form_state['values']['username'];
  $password = $form_state['values']['password'];
  $year_of_birth = $form_state['values']['year_of_birth'];
  $email = $form_state['values']['email'];

  db_insert('registration')->fields(array(
    'firstname' => $firstname,
    'username' => $username,
    'password' => $password,
    'yearofbirth' =>$year_of_birth,
    'email' => $email,
    'file' => $file->uri,))
  ->execute();

}
5
  • thanks it worked but it is saving public://practice_0.jpeg in database is it right
    – bangari
    Commented Jun 12, 2013 at 6:36
  • yep, drupal manages files with stream wrappers, a very useful feature Commented Jun 12, 2013 at 6:39
  • Thankyou so much Cornelius.This is my first task in drupal creating a registration form.You really helped a lot.now i will start with creating login form and make the user to login.if you can give me the suggestion for login iam so glad.Thankyou once again for helping
    – bangari
    Commented Jun 12, 2013 at 6:42
  • You are welcome, Drupal has this functionality out of the box, i suggest you to read more about user management in the docs. Commented Jun 12, 2013 at 6:53
  • ok Thankyou for your suggestion i will go through it
    – bangari
    Commented Jun 12, 2013 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.