So, I've been working at this and searching forums for quite some time and have tried many different options and still can't figure out what the issue is. I've been working with PHP for several years, but I'm still pretty new to Drupal. I'm working on some code that I want to simply take input form a form then insert into a database table called 'dr_rower_results', my code is below:

<?php
/**
 * @file
 * This module allows users to input information for rower race results.
 */
 
$form = drupal_get_form('rower_results_form'); 
 
/**
 * Implements hook_form().
 */
function rower_results_form($form, &$form_state){
	$form = array();	
	
	$form['overview'] = array(
		'#markup' => t('This form allows you to enter results for rowers'),
		'#prefix' => '<h3>',
		'#suffix' => '</h3>',
	);
	
	$form['name'] = array(
		'#title' => t('Rower\'s Name'),
		'#description' => t('Enter the rower\'s name'),
		'#type' => 'textfield',
		'#required' => TRUE,
	);
	
	$form['team'] = array(
		'#title' => t('Rower\'s Team'),
		'#description' => t('Enter the team the rower rows for'),
		'#type' => 'textfield',
		'#required' => TRUE,
	);
	
	//$form['actions'] = array('#type' => 'actions');	
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Submit'),
	);
	
	$form['#submit'][] = 'rower_results_form_submit';
	return $form;
}

/**
 * Validate Rower Results admin settings
 */
function rower_results_form_validate($form, &$form_state){
	//dpm($form_state);
	echo("<script type='text/javascript'>alert('validate');</script>");
	
	// Regular expression for validating
	//$name_regex = 
	
	//$name = $form_state['values']['rower_results_name'];
	
	//if(!preg_match($name_regex, $name)){
	//	form_set_error('rower_results_name', t('invalid name'));
	//}
}

function rower_results_form_submit($form, &$form_state){
	$result = db_insert('dr_rower_results') 
	->fields(array('name' => $form_state['values']['name'], 
						'team' => $form_state['values']['team']
				))->execute();
	drupal_set_message(t('Your form has been submitted'.$args[0].$args[1]));
}

function hook_block_view($delta = ''){
	
}
echo(drupal_render(&$form));


?>

This code is in a block with text format set as PHP code. I believe I have narrowed this down to it being an issue with the db_insert (also tried db_query('INSERT INTO... ). If I remove the db_ function then the form clears and shows the message "Your form has been submitted" followed by my two inputs. If I keep the db_ function there then on submit the page simply brings me to my drupal user page and does not make an insert into the database.

Any help would be much appreciated.

Comments

Jaypan’s picture

You should be putting this in a module, not using the PHP filter on a block. I don't think you can declare form functions with the PHP filter.

If you create a module, you can create a block using hook_block_info() and hook_block_view(). You can use your form in that block.

drewk2525’s picture

Finally got this working in a module. A couple of problems I ran into in case anybody is as new to this as me:
rower_results_form doesn't work because in the .info file the name is = Rower Results
Rower_Results_form solves that problem

db_insert('dr_rower_results') does not work
db_insert( ) automatically adds 'dr_' to the start of the node name, so simply writing:
db_insert('rower_results') when the node is named 'dr_rower_results' works

Also, while a MySQL query may allow an "INSERT INTO" without including values for all fields, the Drupal db_insert( ) requires that ALL fields are specified (unless they have a default value)

Anyways, thanks Jaypan for the help, and I hope this helps somebody who's as lost as I was a week ago!

Here is my working code for anyone interested:



<?php
/**
 * @file
 * This module allows users to input information for rower race results.
 */
 
 function Rower_Results_block_info() {
  // This example comes from node.module.
  $blocks['Rower_Results_form'] = array(
    'info' => t('Rower Results'),
    'cache' => DRUPAL_NO_CACHE,
  );

  return $blocks;
}

function Rower_Results_block_view($delta = '') {
  // This example is adapted from node.module.
  $block = array();

  switch ($delta) {
    case 'Rower_Results_form':
      $block['subject'] = t('Rower Results');
      $block['content'] = drupal_get_form('Rower_Results_form');
      break;
  }
  return $block;
}

 
function Rower_Results_menu(){	
	$items = array();
	$items['Rower_Results'] = array(
		'title' => "Rower Results",
		'page callback' => "Rower_Results_personal_info", // after visit drupal6/person, person_personal_info() function is called
		'access callback' => true, // must return true, otherwise it will not visible as menu item
		'type' => MENU_NORMAL_ITEM, // drupal's default menu type
		'weight' => '10', // we want to display person link below in our nav menu
	);
	return $items; // finally, do not forget to return $items array
}
 
/**
 * Implements hook_form().
 */
function Rower_Results_form($form, &$form_state){
	$form = array();	
	
	$form['overview'] = array(
		'#markup' => t('This form allows you to enter results for rowers'),
		'#prefix' => '<h3>',
		'#suffix' => '</h3>',
	);
	
	$form['name'] = array(
		'#title' => t('Rower\'s Name'),
		'#description' => t('Enter the rower\'s name'),
		'#type' => 'textfield',
		'#required' => TRUE,
	);
	
	$form['team'] = array(
		'#title' => t('Rower\'s Team'),
		'#description' => t('Enter the team the rower rows for'),
		'#type' => 'textfield',
		'#required' => TRUE,
	);
	
	//$form['actions'] = array('#type' => 'actions');	
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Submit'),
	);
	
	$form['#submit'][] = 'Rower_Results_form_submit';
	return $form;
}

/**
 * Validate Rower Results admin settings
 */
function Rower_Results_form_validate($form, &$form_state){
  // No validation yet, but this will cause the form to run this code before submitting
}

function Rower_Results_form_submit($form, &$form_state){	
	$result = db_insert('rower_results')
		->fields(array(
			'rowerName' => $form_state['values']['name'], 
			'rowerTeam' => $form_state['values']['team'],
// The follow are arbitrary values simply to get it working until I finish the form
			'rowerGender' => 'M',
			'erg1_distance' => '2000',
			'erg1_result' => '8:00.0',
			'erg2_distance' => '5000',
			'erg2_result' => '22:00.0'
			))
			->execute();
// This shows a success message with the values inputted into the form
	drupal_set_message(t('Your entry has been submitted: '.$form_state['values']['name'].' '.$form_state['values']['team']));
}

function hook_block_view($delta = ''){
// This displays the form in the block
	echo(drupal_render(&$form));	
}
?>
Jaypan’s picture

Glad you got it working, however there is one issue. You should not use uppercase module machine names, which means the folder that contains the module, as well as all files. One reason is the issue you ran into - if you use capital letters, you have to do the same with your function names. But the other is that some hooks will not work with capital letters in the name. hook_update() is one I believe. This can be really hard to debug, so you would be doing yourself a favor to only use lower-case letters in your module machine names.

So you should rename your module folder to rower_results, and name the files inside accordingly. Note that you should disable and uninstall the module before this, and reinstall afterwards. You cannot do it while the module is enabled on the site.