By maheshg85 on
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
Your edit form is using unset
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
$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
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.
Minor point, you can generate links with l() function like
l(t('Edit'), "doctor/edit/$data->doctorid");I did few changes and form is
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
edit part of code is as follows
please help me the tackle problem to pass that id .
Please put code between <code> and </code> tags: nevets
This $query = "UPDATE
This
should be
or
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.
its working
thanks dude I applied first method its working..
not getting what is $doctor
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");
}