I have wrote custom module for showing values in html format table. I have delete and edit action for every row. My delete function working but edit function not working .Whats wrong with code. What i need to do corrections for it.For editing the form i NEED TO BE get default values in form that too not getting.please help me guys

<?php

function doctor_menu() {
  $items = array();
 
  $items['doctor'] = array(
    'title' => t('Doctors'),
    'page callback' => 'doctors_list',
    'access arguments' => array('access doctor'),
    'type' => MENU_NORMAL_ITEM  );
 $items['doctor/delete/%doctor_user'] = array(
    'title' => t('Delete doctor'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('doctor_delete_confirm', 2),
    'access arguments' => array('access doctor'),
    'type' => MENU_CALLBACK, );
$items['doctor/edit/%doctor_user'] = array(
    'title' => t('Edit Form'),
    'page callback' => 'drupal_get_form',
	'page arguments' => array('doctor_edit_confirm', 2),
   'access arguments' => array('access doctor'),
    'type' => MENU_NORMAL_ITEM,
  );return $items;
}
function doctors_list() {
	$header = array(t('Doctor Name'), t('Gender'),  t('Status'), t('Action'));
 $query = "SELECT * FROM {doctor}";
	$rs = db_query($query);
 $row = array();
 if ($rs) {
		while ($data = db_fetch_object($rs)) {
			$gender = $data->gender;
			$status = $data->status ;
			$row[] = array(stripslashes(ucwords($data->firstname)) . ' ' . stripslashes(ucwords($data->lastname)), $gender, $status, 
			"<a href='doctor/edit/{$data->doctorid}'>" . t('Edit') . "</a> | <a href='doctor/delete/{$data->doctorid}'>" . t('Delete') . "</a>");
		}}
 $str .= theme_table($header, $row);
 return $str;
}
function doctor_user_load($doctorid) {
	$query = "SELECT * FROM {doctor} WHERE doctorid = %d";
	$rs = db_query($query, $doctorid);
 
	if ($rs) {
		while ($data = db_fetch_object($rs)) {
			return $data;
		}}
 return FALSE;
}
function doctor_delete_confirm(&$form_state, $doctor) {
	$form['_doctor'] = array(
		'#type' => 'value',
		'#value' => $doctor,);
 
return confirm_form($form,t('Are you sure you want to delete this doctor?'),
    	isset($_GET['destination']) ? $_GET['destination'] : "doctor",t('This action cannot be undone.'),t('Delete'),t('Cancel'));}

function doctor_delete_confirm_submit($form, &$form_state) {
	$form_values = $form_state['values'];
 if ($form_state['values']['confirm']) {
		$doctor = $form_state['values']['_doctor'];
                                      
                    $query = "DELETE  FROM {doctor} where doctorid=$doctor>doctorid";
	                   $rs = db_query($query);
 drupal_set_message(t('Doctor has been deleted successfully.'));}
 drupal_goto("doctor");
}
function doctor_edit_confirm(&$form_state,$doctor){

 $form = array();
 $form['firstname']=array(
		'#title'=>t('First Name'),
		'#type'=>'textfield',
                                      '#default_value' => $firstname );
$form['lastname']=array(
		'#title'=>t('Last Name'),
		'#type'=>'textfield',
                                     '#default_value' => $lastname);
$form['gender']=array(
		'#title'=>t('Gender'),
		'#type'=>'radios','#options' => array(t('male'), t('female')),
                                     '#default_value' => male);
$form['status']=array(
		'#title'=>t('Status'),
		'#type'=>'radios',
                                     '#default_value' => active,
                                     '#options' => array(t('active'), t('inactive')));
	return confirm_form($form,t(''),
    	isset($_GET['destination']) ? $_GET['destination'] : "doctor",
    	t(''),
    	t('Edit'),
    	t('Cancel'));}

function doctor_edit_confirm_submit($form, &$form_state)
{
$form_values = $form_state['values'];
 if ($form_state['values']['confirm']) {
		$doctor = $form_state['values']['_doctors'];
 $query = "UPDATE {doctor} WHERE doctorid='$doctor->doctorid'";
 } drupal_goto("doctor");}

Reformatted to use <code> and </code> tags: nevets

Comments

nevets’s picture

Your edit form is using unset values for the defaults and the sql update command does not actually update any values.

Lets look at the the edit for first, here is one element

 $form['firstname']=array(
'#title'=>t('First Name'),
'#type'=>'textfield',
                                      '#default_value' => $firstname );

$firstname is not set and I suspect that should be $doctor->firstname.

Also both gender and status use unquoted strings (male, active) which is a bad practice.

As for the update it reads

 $query = "UPDATE {doctor} WHERE doctorid='$doctor->doctorid'";

which is an incomplete UPDATE command (it does not provide the fields or their values to update).

Also, the drupal_goto() is not the best practice there.

Here is an updated submit function that uses drupal_write_record(). This will work if your form element keys are the same as your table field names.

function doctor_edit_confirm_submit($form, &$form_state)
{
  $form_values = $form_state['values'];
  if ($form_values['confirm']) {
     drupal_write_record('doctor', $form_values, array('doctorid'));
  }
  $form_state['redirect'] = "doctor";
}

Minor point, you can generate links with l() function like l(t('Edit'), "doctor/edit/$data->doctorid");

maheshg85’s picture

I did few changes and form is getting edited when i put hard core doctorid like doctorid=2 but when i paasing through variable its showing following error

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: UPDATE doctor SET firstname = 'Sachin', lastname= 'abc', gender = 'male', status = 'active' WHERE doctorid= in D:\FR-Server\Apache2\htdocs\fr\sites\all\Modules\doctor\doctor.module on line 157.

edit part of code is as follows

function doctor_edit_confirm(&$form_state,$doctor){
 $form = array();
$form['_doctor'] = array(
		'#type' => 'value',
		'#value' => $doctor,);
 $form['firstname']=array(
		'#title'=>t('First Name'),
		'#type'=>'textfield',);
$form['lastname']=array(
		'#title'=>t('Last Name'),
		'#type'=>'textfield',);
$form['gender']=array(
		'#title'=>t('Gender'),
		'#type'=>'radios',
                  '#options' => array(t('male'), t('female')),);
$form['status']=array(
		'#title'=>t('Status'),
		'#type'=>'radios',
                                     '#default_value' => active,
                                     '#options' => array(t('active'), t('inactive')),);
return confirm_form($form,t(''),
    	isset($_GET['destination']) ? $_GET['destination'] : "doctor",
    	t(''),t('Edit'),t('Cancel'));}
function doctor_edit_confirm_submit($form, &$form_state)
{
if ($form_state['values']['confirm']) {
$doctor = $form_state['values']['_doctors'];
 $firstname = $form_state['values']['firstname'];
    $lastname = $form_state['values']['lastname'];
    $gender1= $form_state['values']['gender'];
$gender =  $gender1 ? t('female') : t('male');
    $status1 = $form_state['values']['status'];
$status = $status1 ? t('inactive') : t('active');
$query = "UPDATE  {doctor} SET firstname = '$firstname', lastname= '$lastname', gender = '$gender', status = '$status'  WHERE  doctorid=$doctor->doctorid";
 $rs = db_query($query); }
drupal_goto("doctor");
}

please help me the tackle problem to pass that id .

Please put code between <code> and </code> tags: nevets

nevets’s picture

This

$query = "UPDATE  {doctor} SET firstname = '$firstname', lastname= '$lastname', gender = '$gender', status = '$status'  WHERE  doctorid=$doctor->doctorid";
$rs = db_query($query);

should be

$query = "UPDATE  {doctor} SET firstname = '$firstname', lastname= '$lastname', gender = '$gender', status = '$status'  WHERE  doctorid=%d";
$rs = db_query($query, $doctor->doctorid);

or

$doctor = $form_state['values']['_doctors'];
$form_state['values']['doctorid'] = $doctor->doctorid;
drupal_write_record('doctor', $form_state['values'], 'doctorid');

Additional notes:
For a function that edits existing records it really should be setting '#default value' for each element.

#option arrays should have key/value pairs. Example: array(1 => t('active'), 0 => t('inactive')),);

You should not store already translated values in the database.

It is also a better practice to store the keys and not the values.

maheshg85’s picture

thanks dude I applied first method its working..

brajendrashukla’s picture

function doctor_delete_confirm(&$form_state, $doctor) {
$form['_doctor'] = array(
'#type' => 'value',
'#value' => $doctor,);

return confirm_form($form,t('Are you sure you want to delete this doctor?'),
isset($_GET['destination']) ? $_GET['destination'] : "doctor",t('This action cannot be undone.'),t('Delete'),t('Cancel'));}

function doctor_delete_confirm_submit($form, &$form_state) {
$form_values = $form_state['values'];
if ($form_state['values']['confirm']) {
$doctor = $form_state['values']['_doctor'];

$query = "DELETE FROM {doctor} where doctorid=$doctor>doctorid";
$rs = db_query($query);
drupal_set_message(t('Doctor has been deleted successfully.'));}
drupal_goto("doctor");
}