Hey,

I started working on a (my first) custom form module, which purpose is to edit existing non-Drupal database tables. The current code is based on examples from the Pro Drupal Development book and this post I found on drupal.org. After 2 days of struggling to get the submit function working, I was hoping maybe one of you guys could point me in the right direction.
The contents of my .module is as follows:

// $Id$

/**
 * @file
 * Module for editing db-tables with maintenance objects.
 * Inspiration and major part of the code: http://drupal.org/node/1019526
 */

/**
 * Implementation of hook_perm().
 */
function form_ldl_perm() {
  return array('query database');
}

/**
 * Implementation of hook_menu().
 */
function form_ldl_menu() {
  $items['landgoed/landgoed-de-laan/bevraag-db'] = array(
    'title' => 'Wijzig beheerobjecten',
    'page callback' => 'form_ldl_query_db',
    'access callback' => 'user_access',
    'access arguments' => array('query database'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['landgoed/landgoed-de-laan/bevraag-db/edit/%beheerobject'] = array(
    'title' => t('Edit Form'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('beheerobject_edit_confirm', 4),
    'access arguments' => array('query database'),
    'type' => MENU_NORMAL_ITEM,
  );  
  
  return $items;
}

/**
 * Page callback for the overview page where you can select maintenance objects.
 */
function form_ldl_query_db() {
  $header = array(t('Code'), t('Waterkeur'),  t('Lengte'), t('Breedte'));
  $query = "SELECT * FROM {ldlwlijn}";
  $rs = db_query($query);
  $row = array();
  if ($rs) {
    while ($data = db_fetch_object($rs)) {
      $code = $data->code;
      $waterkeur = $data->waterkeur;
      $lengte = $data->lengte;
      $breedte = $data->breedte;
      $row[] = array($code, $waterkeur, $lengte, $breedte, l(t('Edit'), "landgoed/landgoed-de-laan/bevraag-db/edit/$data->code"));
    }
  }
  $str .= theme_table($header, $row);
  return $str;
}

/**
 * Implementation of hook_load() (as far as I know now)
 */

function beheerobject_load($beheerobject) {
  $query = "SELECT * FROM {ldlwlijn} WHERE code = '%s'";
  $rs = db_query($query, $beheerobject);
  if ($rs) {
    while ($data = db_fetch_object($rs)) {
    return $data;
    }
  }
  return FALSE;
}

/**
 * Function called on load of the edit form page.
 */

function beheerobject_edit_confirm(&$form_state,$beheerobject){
  $form = array();
 
  $form['waterkeur']=array(
    '#title'=>t('Waterkeur'),
    '#type'=>'textfield',
    '#default_value' => $beheerobject->waterkeur 
  );
  
  $form['lengte']=array(
    '#title'=>t('Lengte'),
    '#type'=>'textfield',
    '#default_value' => $beheerobject->lengte 
  );
    
  $form['breedte']=array(
    '#title'=>t('Breedte'),
    '#type'=>'textfield',
    '#default_value' => $beheerobject->breedte
  );
  
  return confirm_form($form,t(''),
    isset($_GET['destination']) ? $_GET['destination'] : "landgoed/landgoed-de-laan/bevraag-db",
    t(''),
    t('Edit'),
    t('Cancel'));
  }

/**
 * Implementation of submit function. (function name + _submit)
 */

function beheerobject_edit_confirm_submit($form, &$form_state)
{
  if ($form_values['confirm']) {
     $waterkeur = $form_state['values']['waterkeur'];
     $lengte = $form_state['values']['lengte'];
     $breedte = $form_state['values']['breedte'];
     
     $query = "UPDATE {ldlwlijn} SET waterkeur = '$waterkeur', lengte= '$lengte', breedte = '$breedte' WHERE code='%s'";
     $rs = db_query($query, $beheerobject->code);
  }
  $form_state['redirect'] = "landgoed/landgoed-de-laan/bevraag-db";
}

I'm a newbie to custom module development, so if you need more info, please don't hesitate to ask. I would be very grateful if someone could point into the right direction!

Thank you in advance!

MF

Comments

abhishek sawant’s picture

Check below code if it works or not :


function form_ldl_perm() {
  return array('query database');
}

function form_ldl_menu() {
	$items = array();
	$items['landgoed/landgoed-de-laan/bevraag-db'] = array(
	    'title' => 'Wijzig beheerobjecten',
	    'page callback' => 'form_ldl_query_db',
	    'access arguments' => array('query database'),
        'type' => MENU_NORMAL_ITEM,
  	);	
  	$items['landgoed/landgoed-de-laan/bevraag-db/%/edit'] = array(
		'title' => 'Edit Form',
	    'page callback' => 'form_ldl_admin_edit',
	    'access arguments' => array('query database'),
	    'page arguments' => array(3),
	    'type' => MENU_NORMAL_ITEM,
	);	
	return $items;
}

function form_ldl_query_db() {
	$output = '';
	$edit = array();
	$output .= drupal_get_form('form_ldl_form',$edit);
	$sql = "SELECT * FROM {ldlwlijn}";
	$result = db_query($sql);
	$rows = array();
	$header = array(t('Code'), t('Waterkeur'), t('Lengte'), t('Breedte'));
	while ($data = db_fetch_object($result)) {
		$row = array();
		$row[] = $data->code;
		$row[] = $data->waterkeur;
		$row[] = $data->lengte;
		$row[] = $data->breedte;
		$row[] = l('Edit', 'landgoed/landgoed-de-laan/bevraag-db/'.$data->code.'/edit');
		$rows[] = $row;
	}
	$output .= theme('table', $header, $rows);
	return $output;
}

function form_ldl_form(&$form_state,$edit=array()){
    if(!$edit['code']) {
		$form['custom'] = array(
		  '#type' => 'fieldset',
		  '#title' => t('Add new custom'),
		  '#collapsible' => TRUE,
		  '#collapsed' => TRUE,
		);
	}
	else {
		$form['custom'] = array(
		  '#type' => 'fieldset',
		  '#title' => $edit['waterkeur'],
		  '#collapsible' => FALSE,
		);
		$form['custom']['code'] = array(
		  '#type' => 'hidden',
		  '#value' => $edit['code'],
		  '#weight' => 9,
		);
	}
	$form['custom']['waterkeur'] = array(
		'#title' => t('Waterkeur'),
		'#type' => 'textfield',
		'#default_value' => $edit['waterkeur'],
	);
  	$form['custom']['lengte'] = array(
		'#title' => t('Lengte'),
		'#type' => 'textfield',
		'#default_value' => $edit['lengte'],
	);
   	$form['custom']['breedte'] = array(
		'#title'=> t('Breedte'),
		'#type'=> 'textfield',
		'#default_value' => $edit['breedte'],
	);
    $form['custom']['save'] = array(
	  '#type' => 'submit',
	  '#value' => 'Save',
	  '#weight' => 9,
	);
    return $form;    
}

function form_ldl_form_submit($form, &$form_state) {
		$waterkeur = $form_state['values']['waterkeur'];
		$lengte = $form_state['values']['lengte'];
		$breedte = $form_state['values']['breedte'];
		if($code = $form_state['values']['code']) { // update
			db_query("UPDATE {ldlwlijn} SET 
			waterkeur = '$waterkeur',
			lengte = '$lengte',
			breedte = '$breedte'
			WHERE code = $code");
			drupal_set_message("Waterkeur <b>$waterkeur</b> is updated");
		}
		else { // insert
			db_query("INSERT INTO {ldlwlijn} 
			(waterkeur, lengte, breedte) 
			VALUES 
			('$waterkeur', '$lengte', '$breedte)");
			drupal_set_message("Waterkeur <b>$waterkeur</b> is added");
		}
		cache_clear_all();
		$form_state['redirect'] = 'landgoed/landgoed-de-laan/bevraag-db';
	return;
}

function form_ldl_admin_edit($id=0) {
	if($id) {
		$sql = "SELECT * FROM {ldlwlijn} WHERE code = $id";
		$result = db_query($sql);
		$data = db_fetch_object($result);
		
		$edit = array();
		$edit['code'] = $data->code;
		$edit['waterkeur'] = $data->waterkeur;
		$edit['lengte'] = $data->lengte;
		$edit['breedte'] = $data->breedte;
		}
	return drupal_get_form('form_ldl_form',$edit);
}

Might it helps...

Abhishek Sawant
Drupal Developer

SidneyGijzen’s picture

It sure was of help! After some small changes to your code (since my id is a textfield, i put the "id" en "code" variables in quotes here and there), it works perfectly.
I need to study your code a bit more, so I can understand exactly what you're doing and where. I hope (if needed), I can ask you some questions about it in this topic in the future about it. :)

For now; thanks a LOT! You really made my day!

MF

abhishek sawant’s picture

Yes sure, Its my pleasure to help..:)

Abhishek Sawant
Drupal Developer

smilysarat’s picture

Hi, i have attached a Zip a file... with my functionality including Db files...... Please help me, i have to do this same functionality in Drupal 7.8... How can i implement this... Please i have tried but not yet..
i am new to drupal, Please help its urgent for me.... http://drupal.org/node/1288472 Please chk this link for my attach file...
Thanks a lot

SidneyGijzen’s picture

hey,
I would like to help you, but unfortunately you're developing in Drupal 7 (D7). The form which I'm using is suited for Drupal 6. I heard quite a lot has changed in the API for D7 and since I haven't worked with D7 yet, I'm not able to help you at this moment in time.
I suggest you start a new thread in this forum (clearly marking it as D7).

MF

smilysarat’s picture

Hii...Thanks for ur prompt response on this... Please guide me for that in 6 what u know at present so that i will try to work on 7... based on that.

Thank u soo.. much

SidneyGijzen’s picture

Well...actually the second post in this topic (from Abishek) is a piece of code which works perfectly. So, you can compare that to your code. Just think in your tables and fields...

However, again, the Form API has changed quite a lot in 7, see this series of posts of fellow drupaller Randy Fay

smilysarat’s picture

Hii.. it is successfully running for me.. thanks for ur help... i have an another doubt on this we have a select drop down in user form... Whatever the user selects, i want to get that value and also i want to get text field value too to compare Both values in the Data Base. Is there any easy way to this... Or else how do we do this to get that values
Please help me.. Thanks once again..

SidneyGijzen’s picture

You're welcome. Good to hear you got it working.

In the Drupal 6 Form API you can build select lists as follows...
Include the following in your form

      $form['custom']['example_options'] = array(
        '#type' => 'value',
        '#value' => array(0 => 'value1', 1 => 'value2', 2 => 'value3')
    );

      $form['custom']['example'] = array(
        '#title' => t('Example'),
        '#type' => 'select',
        '#options' => $form['custom']['example_options']['#value']
    );

To retrieve the selected value on submit, include the following in your submit function

        $example_key = $form_state['values']['example'];
        $example = $form_state['values']['example_options'][$example_key];

So, the variable "$example" has the selected value and can be used in your update or insert statement for the db_query.

I hope this example will give you enough to go on and get it working in D7!

smilysarat’s picture

Thanks for ur great help friend..... Both user and admin forms are working very well...... Thanks for ur Suggestions....

SidneyGijzen’s picture

Good to hear you got everything working! :)

bsmith451’s picture

function demo_admin_edit($id=0) {
   if($id) {
       $sql = "SELECT * FROM {test_mysql} WHERE id = $id";

If I remove the if ($id) in this I can get the edit to work. I've changed the pathing and I'm wondering how you are passing the id var from the 'EDIT' click to this function. I think that is where I'm getting fouled up.

abhishek sawant’s picture

Refer form_ldl_form() function where we passing an hidden id on edit condition.

Abhishek Sawant
Drupal Developer

bsmith451’s picture

I was having problems with the ?q= portion of the url in my WAMP install. Testing on my stage server shows that this is working. Thanks for the heads up of where to look for the var, that got me to the answer.

smilysarat’s picture

In Wamp server by default it enables clean urls... So.. just disable clean urls in site configuration. For better, If u use Xampp then u never face this issue..

smilysarat’s picture

Hii...... Please help me if you have any idea about this...
I implemented Upcoming Events in version 6. now if i want to display the selected events in "Whats New" Block then what should i do..? Any Idea ?

WorldFallz’s picture

This is not a code question and has nothing to do with the topic of this post-- please don't hijack others' threads. They're free-- create your own for the new topic in the appropriate forum (only code questions belong in this forum).