First, your update query is not safe from SQL injection. Please learn how to use db_query properly <a href="http://api.drupal.org/api/drupal/includes--database.pgsql.inc/function/db_query/6">http://api.drupal.org/api/drupal/includes--database.pgsql.inc/function/db_query/6</a><div>
<br></div><div>Second, you have a typo -- should be '_doctor' not '_doctors' in your _submit() function.</div><div><br></div><div>Default values are clearly explained here <a href="http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#default_value">http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#default_value</a></div>
<div><br></div><div>Please *read the documentation* and try to solve your own PHP bugs before posting to this list in the future.</div><div><br></div><div><br><div class="gmail_quote">On Mon, Jan 10, 2011 at 2:26 AM, mahesh gajabar <span dir="ltr"><<a href="mailto:mahesh143an@gmail.com">mahesh143an@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi <br>I have wrote a module for despalying my database table data into html format table. I have delete and edit button infront of every button. delete function is working fine. Code is as below<br>
<br><br><br><div style="margin-left:40px;font-family:verdana,sans-serif">
<i><b><?php</b></i><br><br><i><b>function doctor_menu() {</b></i><br><i><b> $items = array();</b></i><br><i><b> </b></i><br><i><b> $items['doctor'] = array(</b></i><br><i><b> 'title' => t('Doctors'),</b></i><br>
<i><b> 'page callback' => 'doctors_list',</b></i><br><i><b> 'access arguments' => array('access doctor'),</b></i><br><i><b> 'type' => MENU_NORMAL_ITEM,</b></i><br>
<i><b> );</b></i><br><i><b> </b></i><br><i><b> $items['doctor/delete/%doctor_user'] = array(</b></i><br><i><b> 'title' => t('Delete doctor'),</b></i><br><i><b> 'page callback' => 'drupal_get_form',</b></i><br>
<i><b> 'page arguments' => array('doctor_delete_confirm', 2),</b></i><br><i><b> 'access arguments' => array('access doctor'),</b></i><br><i><b> 'type' => MENU_CALLBACK,</b></i><br>
<i><b> );</b></i><br><i><b> </b></i><br><i><b> $items['doctor/edit/form'] = array(</b></i><br><i><b> 'title' => t('doctor'),</b></i><br><i><b> 'page callback' => 'drupal_get_form',</b></i><br>
<i><b> 'page arguments' => array('doctor_page',2),</b></i><br><i><b> 'access arguments' => array('access doctor'),</b></i><br><i><b> 'type' => MENU_NORMAL_ITEM,</b></i><br>
<i><b> );</b></i><br><br><i><b>$items['doctor/edit/%doctor_user'] = array(</b></i><br><i><b> 'title' => t('Edit Form'),</b></i><br><i><b> 'page callback' => 'drupal_get_form',</b></i><br>
<i><b> 'page arguments' => array('doctor_edit_confirm', 2),</b></i><br><i><b> 'access arguments' => array('access doctor'),</b></i><br><i><b> 'type' => MENU_NORMAL_ITEM,</b></i><br>
<i><b> );</b></i><br><i><b> return $items;</b></i><br><i><b>}</b></i><br><br><i><b>function doctors_list() {</b></i><br><i><b> $header = array(t('Doctor Name'), t('Gender'), t('Status'), t('Action'));</b></i><br>
<i><b> </b></i><br><i><b> $query = "SELECT * FROM {doctor}";</b></i><br><i><b> $rs = db_query($query);</b></i><br><i><b> </b></i><br><i><b> $row = array();</b></i><br><i><b> </b></i><br><i><b> if ($rs) {</b></i><br>
<i><b> while ($data = db_fetch_object($rs)) {</b></i><br><i><b> $gender = $data->gender;</b></i><br><i><b> $status = $data->status ;</b></i><br><i><b> $row[] = array(stripslashes(ucwords($data->firstname)) . ' ' . stripslashes(ucwords($data->lastname)), $gender, $status, </b></i><br>
<i><b> "<a href='doctor/edit/{$data->doctorid}'>" . t('Edit') . "</a> | <a href='doctor/delete/{$data->doctorid}'>" . t('Delete') . "</a>");</b></i><br>
<i><b> }</b></i><br><i><b> }</b></i><br><i><b> </b></i><br><i><b> $str .= theme_table($header, $row);</b></i><br><i><b> </b></i><br><i><b> return $str;</b></i><br><i><b>}</b></i><br><i><b>function doctor_user_load($doctorid) {</b></i><br>
<i><b> $query = "SELECT * FROM {doctor} WHERE doctorid = %d";</b></i><br><i><b> $rs = db_query($query, $doctorid);</b></i><br><i><b> </b></i><br><i><b> if ($rs) {</b></i><br><i><b> while ($data = db_fetch_object($rs)) {</b></i><br>
<i><b> return $data;</b></i><br><i><b> }</b></i><br><i><b> }</b></i><br><i><b> </b></i><br><i><b> return FALSE;</b></i><br><i><b>}</b></i><br><br><br><i><b>function doctor_delete_confirm(&$form_state, $doctor) {</b></i><br>
<i><b> $form['_doctor'] = array(</b></i><br><i><b> '#type' => 'value',</b></i><br><i><b> '#value' => $doctor,</b></i><br><i><b> );</b></i><br><i><b> </b></i><br><i><b> return confirm_form($form,</b></i><br>
<i><b> t('Are you sure you want to delete this doctor?'),</b></i><br><i><b> isset($_GET['destination']) ? $_GET['destination'] : "doctor",</b></i><br><i><b> t('This action cannot be undone.'),</b></i><br>
<i><b> t('Delete'),</b></i><br><i><b> t('Cancel'));</b></i><br><i><b>}</b></i><br><br><i><b>function doctor_delete_confirm_submit($form, &$form_state) {</b></i><br><i><b> $form_values = $form_state['values'];</b></i><br>
<i><b> </b></i><br><i><b> if ($form_state['values']['confirm']) {</b></i><br><i><b> $doctor = $form_state['values']['_doctor'];</b></i><br><i><b> </b></i><br>
<i><b> $query = "DELETE FROM {doctor} where doctorid=$doctor->doctorid";</b></i><br><i><b> $rs = db_query($query);</b></i><br><i><b> </b></i><br><i><b> //doctor_delete($form_state['values'], $doctor->doctorid); </b></i><br>
<i><b> </b></i><br><i><b> drupal_set_message(t('Doctor has been deleted successfully.'));</b></i><br><i><b> }</b></i><br><i><b> </b></i><br><i><b> drupal_goto("doctor");</b></i><br><i><b>}</b></i><br>
<br><i><b>function doctor_edit_confirm(&$form_state,$doctor){</b></i><br><i><b> $form = array();</b></i><br><i><b>$form['_doctor'] = array(</b></i><br><i><b> '#type' => 'value',</b></i><br>
<i><b> '#value' => $doctor,</b></i><br><i><b> );</b></i><br><i><b> $form['firstname']=array(</b></i><br><i><b> '#title'=>t('First Name'),</b></i><br><i><b> '#type'=>'textfield',</b></i><br>
<i><b> //'#prefix'=>'<table class="formtable"><tr><td class="labeltext">First Name</td><td class="textbox">',</b></i><br>
<i><b> '#suffix'=>'</td></tr>',</b></i><br><i><b> );</b></i><br><i><b>$form['lastname']=array(</b></i><br><i><b> '#title'=>t('Last Name'),</b></i><br>
<i><b> '#type'=>'textfield',</b></i><br><i><b> '#default_value' => $lastname,</b></i><br><i><b> );</b></i><br><i><b>$form['gender']=array(</b></i><br>
<i><b> '#title'=>t('Gender'),</b></i><br><i><b> '#type'=>'radios',</b></i><br><i><b> </b></i><br><i><b> '#options' => array(t('male'), t('female')),</b></i><br>
<i><b> '#default_value' => male,</b></i><br><i><b> );</b></i><br><i><b>$form['status']=array(</b></i><br><i><b> '#title'=>t('Status'),</b></i><br>
<i><b> '#type'=>'radios',</b></i><br><i><b> '#default_value' => active,</b></i><br><i><b> '#options' => array(t('active'), t('inactive')),</b></i><br>
<i><b> </b></i><br><i><b> );</b></i><br><i><b> </b></i><br><i><b>return confirm_form($form,t(''),</b></i><br><i><b> isset($_GET['destination']) ? $_GET['destination'] : "doctor",</b></i><br>
<i><b> t(''),</b></i><br><i><b> t('Edit'),</b></i><br><i><b> t('Cancel'));</b></i><br><i><b>}</b></i><br><br><i><b>function doctor_edit_confirm_submit($form, &$form_state)</b></i><br>
<i><b>{</b></i><br><i><b>if ($form_state['values']['confirm']) {</b></i><br><i><b>$doctor = $form_state['values']['_doctors'];</b></i><br><i><b> $firstname = $form_state['values']['firstname'];</b></i><br>
<i><b> $lastname = $form_state['values']['lastname'];</b></i><br><i><b> $gender1= $form_state['values']['gender'];</b></i><br><i><b>$gender = $gender1 ? t('female') : t('male');</b></i><br>
<i><b> $status1 = $form_state['values']['status'];</b></i><br><i><b>$status = $status1 ? t('inactive') : t('active');</b></i><br><br><br><i><b>$query = "UPDATE {doctor} SET firstname = '$firstname', lastname= '$lastname', gender = '$gender', status = '$status' WHERE doctorid=$doctor->doctorid";</b></i><br>
<i><b> $rs = db_query($query); </b></i><br><i><b> </b></i><br><i><b> }</b></i><br><i><b>drupal_goto("doctor");</b></i><br><i><b>}</b></i><br><br><div style="text-align:left"><br></div></div>when i put hardcoded value infront of doctorid like doctorid=2 then its getting edited after i click on edit button but when i paasing it using varible and clicking edit button then its showing following error where firstname ->anitha,lastname->abc are the values i enetering in form while editing.<br>
<br><i style="font-family:verdana,sans-serif"><b>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 = 'anitha', lastname= 'abc', gender =
'female', status = 'inactive' WHERE doctorid= in
D:\FR-Server\Apache2\htdocs\fr\sites\all\Modules\doctor\doctor.module on
line 157.</b></i><br><br>my html table on drupal screen <br><table><thead><tr><th>Doctor Name</th><th>Gender</th><th>Status</th><th>Action</th> </tr></thead>
<tbody>
<tr><td>Mahesh Gajabar</td><td>male</td><td>inactive </td><td><a href="http://localhost/fr/doctor/edit/1" target="_blank">Edit</a> | <a href="http://localhost/fr/doctor/delete/1" target="_blank">Delete</a></td> </tr>
<tr><td>Anitha Patil</td><td>female</td><td>active</td><td><a href="http://localhost/fr/doctor/edit/2" target="_blank">Edit</a> | <a href="http://localhost/fr/doctor/delete/2" target="_blank">Delete</a></td> </tr>
<tr><td>Pavithra M</td><td>female</td><td>active</td><td><a href="http://localhost/fr/doctor/edit/3" target="_blank">Edit</a> | <a href="http://localhost/fr/doctor/delete/3" target="_blank">Delete</a></td> </tr>
<tr><td>Ravi Mandayam</td><td>male</td><td>active</td><td><a href="http://localhost/fr/doctor/edit/4" target="_blank">Edit</a> | <a href="http://localhost/fr/doctor/delete/4" target="_blank">Delete</a></td></tr></tbody></table>
<br>edit form :<br>
<br>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman","serif"">First Name: <input maxlength="128" size="60" name="firstname" type="text"></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman","serif"">Last Name: <input maxlength="128" size="60" name="lastname" type="text"></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman","serif"">Gender: </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman","serif""><input name="gender" value="0" type="radio">male
</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman","serif""><input name="gender" value="1" type="radio">female
</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman","serif"">Status: </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman","serif""><input name="status" value="0" type="radio">active
</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman","serif""><input name="status" value="1" type="radio">inactive
</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman","serif""><span><input name="confirm" value="1" type="hidden"></span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman","serif""><input value="Edit" name="op" type="submit"><a href="http://localhost/fr/doctor" target="_blank"><span style="color:blue">Cancel</span></a></span></p>
<p class="MsoNormal"> </p>
<br>Please help me how i can pass that doctorid and also help me for getting default values in form when i click edit button in table. <br><font color="#888888"><br><br><span style="color:rgb(34, 34, 34);font-family:'Lucida Grande','DejaVu Sans','Bitstream Vera Sans',Verdana,Arial,sans-serif;font-size:13px;line-height:18px"><p style="border-width:0px;background-color:transparent;font-size:13px;margin:0px 0px 0.692em;padding:0px;vertical-align:baseline">
<span style="border-collapse:separate;color:rgb(0, 0, 0);font-family:'Times New Roman';font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;font-size:medium"><span style="font-family:arial;font-size:small"><b><i style="color:rgb(0, 153, 0);font-family:'comic sans ms',sans-serif"><font style="color:rgb(0, 153, 0)" size="2">MAHESH GAJABAR</font></i><br style="color:rgb(0, 153, 0);font-family:'comic sans ms',sans-serif">
<font style="color:rgb(0, 153, 0);font-family:'comic sans ms',sans-serif" size="2"><i>Software Developer<br>FrontalRain Technologies,Bengaluru</i></font></b><br></span></span></p><p style="border-width:0px;background-color:transparent;font-size:13px;margin:0px 0px 0.692em;padding:0px;vertical-align:baseline">
</p></span><br><br>
</font></blockquote></div><br></div>