I am trying to edit/delete any record from the table on clicking on them. The table is in custom form module. For getting the help I came out with this link. There they are controlling the rights to do it by user's role(I guess). In my case I want it to available for everyone. I am new to Drupal so,I am not able to get any major luck with that. I am to delete any record but for editing still no luck. So till now this is what I have coded.

<?php
function products_menu(){
 $items = array();
	 $items['products_form'] = array(
		'title' => t('Products'),
		'page callback' => 'products_list',
		'access callback' => TRUE,
	);
	$items['products_form/delete/%'] = array(
		'title' => t('Delete Product'),
		'page callback' => 'drupal_get_form',
		'page arguments' => array('products_delete_confirm', 2),
		'access callback' => TRUE,
		'type' => MENU_CALLBACK, 
	);
	$items['products_form/edit/%'] = array(
		'title' => t('Edit Product'),
		'page callback' => 'drupal_get_form',
		'page arguments' => array('products_edit_confirm', 2),
	    'access callback' => TRUE,
		'type' => MENU_NORMAL_ITEM,
    );
	
 return $items;
}


function products_list() {
	$header = array('CODE','NAME','OBJECT TYPE',' ');
	$results = db_query("SELECT code,name,object_type FROM {products}");
	$rows = array();
	foreach($results as $key) {
		$code = $key->code;
		$name = $key->name;
		$object_type = $key->object_type;
	  $rows[] = array($code,$name,$object_type,"<a href='products_form/edit/{$key->code}'>" . t('Edit') . "</a> | <a href='products_form/delete/{$key->code}'>" . t('Delete') . "</a>");
	}
	return theme('table', array('header' => $header, 'rows' => $rows));
}


function products_delete_confirm($form ,&$form_state, $product_code) {
$form['_product'] = array(
		'#type' => 'value',
		'#value' => $product_code,);
		
		drupal_set_message($product_code);
		
return confirm_form($form,t('Are you sure you want to delete '.$product_code.' Product?'),
    	isset($_GET['destination']) ? $_GET['destination'] : "products_form",t('This action cannot be undone.'),t('Delete'),t('Cancel'));
		
}


function products_delete_confirm_submit($form, &$form_state) {
	$form_values = $form_state['values'];
 if ($form_state['values']['confirm']) {
$products = $form_state['values']['_product'];
               drupal_set_message(t('Product ' .$products.' will get deleted.'));
		                        
                    $result = db_query("DELETE FROM {products} where code='{$products}'");
 drupal_set_message(t('Products has been deleted successfully.'));}
 drupal_goto("products_form");
}
function products_edit_confirm($form ,&$form_state, $product_code){
drupal_set_message($product_code);
$code = '';$name='';$object_type='';
$results = db_query("SELECT code,name,object_type FROM {products} WHERE code='{$product_code}'");
	foreach($results as $key) {
		$code = $key->code;
		$name = $key->name;
		$object_type = $key->object_type;
	}
$form = array();

	 $form['code']=array(
			'#title'=>t('CODE'),
			'#type'=>'textfield',
			'#value' => $code,);
	$form['name']=array(
			'#title'=>t('NAME'),
			'#type'=>'textfield',
			'#value' => $name,);
	$form['object_type']=array(
			'#title'=>t('OBJECT TYPE'),
			'#type'=>'textfield',
			'#value' => $object_type,);

	return confirm_form($form,t(''),
			isset($_GET['destination']) ? $_GET['destination'] : "products_form",
			t(''),t('Edit'),t('Cancel'));
}


function products_edit_confirm_submit($form, &$form_state)
{
$form_values = $form_state['values'];
if ($form_state['values']['confirm']) {
    
	$code = $form_state['values']['code'];
    $name = $form_state['values']['name'];
    $object_type= $form_state['values']['object_type'];

	$rs = db_query("UPDATE  {products} SET name= '$name', object_type = '$object_type' WHERE  code='{$code}'");
	 drupal_set_message(t('Products has been updated successfully.'));
	 }
	drupal_goto("products_form");
}
 

Comments

VivekTamta’s picture

I already got the solutions...

Instead of this

$form['code']=array(
            '#title'=>t('CODE'),
            '#type'=>'textfield',
            '#value' => $code,);
    $form['name']=array(
            '#title'=>t('NAME'),
            '#type'=>'textfield',
            '#value' => $name,);

I tried this...

$form['code']=array(
                '#title'=>t('CODE'),
                '#type'=>'textfield',
                '#default_value' => $code,
            );
    $form['name']=array(
                '#title'=>t('NAME'),
                '#type'=>'textfield',
                '#default_value' => $name,
            );

Now its working :)

To know about more properties visit this site
https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.h...