Below is my form submit statement for my site. I know that the query works by it self however it does not seem to be setting the variable correctly. Does anybody see why this would not work, or am I going about this the wrong way? Basically my module uploads a file and then on submit this function query's the DB for the upload path and needs to set that value to be a drupal variable. Any Ideas?

function background_audio_form_submit($form, $form_state) {
$result = db_query("SELECT uri
                   FROM `file_managed`
                   WHERE `filemime` = 'audio/mpeg'
                   LIMIT 1");

return $result;

variable_set('background_audio_upload_path', $result-->uri);

}
link|improve this question

feedback

1 Answer

up vote 2 down vote accepted

A couple of things:

  • You're returning the $result variable from the function before calling variable_set so that bit of code will never run.
  • You have a slight syntax error in your variable set code, $result-->uri (you should change it to: $result->uri)

This code should work:

function background_audio_form_submit($form, $form_state) {
  $result = db_query("SELECT uri
               FROM `file_managed`
               WHERE `filemime` = 'audio/mpeg'
               LIMIT 1");

  variable_set('background_audio_upload_path', $result->uri);
}

The return from a form submit function is never used in Drupal so there's no need to perform the return at all.

EDIT

Just one other thing, if you're uploading the file in a custom form using a managed_file element then you'll have the file ID of the uploaded file available in your submit function. You could then use file_load() to get the file object and URI without having to use a custom query. Assuming your element is defined as $form['audio_file'] the following should work:

function background_audio_form_submit($form, $form_state) {
  $fid = $form_state['values']['audio_file'];

  $file = file_load($fid);

  variable_set('background_audio_upload_path', $file->uri);
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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