2016-12-05 62 views
0

我有三個字段:名字,姓氏和出生年份。在代碼中,當我按下提交按鈕時,我很容易插入到數據庫中,所以當我想單擊編輯按鈕時,我想更新這些字段。請任何人都幫我編寫這個更新來將插入的數據放入數據庫中,或者請發佈代碼。如何使用drupal7中的自定義表單更新字段

<?php 
function my_module_menu() { 

    $items = array(); 
    $items['my_module/form'] = array(
    'title' => 'My form', 
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('my_module_my_form'), 
    'access arguments' => array('access content'), 
    'description' => 'My form', 
    'type' => MENU_CALLBACK, 
); 
    $items['my_module/edit'] = array(
    'title' => t('Edit Name'), 
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('my_module_edit'), 
    'access callback' => TRUE, 
    'type' => MENU_CALLBACK, 
); 
return $items; 
} 

function my_module_my_form($form_state) { 
    $form['first_name'] = array(
    '#type' => 'textfield', 
    '#title' => t('First name'), 
    '#required' => TRUE, // Added 
); 
$form['last_name'] = array(
    '#type' => 'textfield', 
    '#title' => t('Last name'), 
    '#required' => TRUE, // Added 
); 
    $form['year_of_birth'] = array(
    '#type' => 'textfield', 
    '#title' => ('Year of birth'), 
    '#description' => 'Format is "YYYY"', 
); 

    // Adds a simple submit button that refreshes the form and clears its contents -- this is the default behavior for forms. 
$form['submit'] = array(
    '#type' => 'submit', 
    '#value' => 'Submit', 
); 

function my_module_edit($form, &$form_submit){ 

$form['Edit'] = array(
    '#type' => 'Edit', 
    '#value' => 'Edit', 
); 

return $form; 

} 
function my_module_my_form_submit($form, &$form_state){ 
    $firstName = $form_state['values']['fname']; 
    $lastName=$form_state['value']['lname']; 
    $yearofbirth = $form_state['values']['yearOfbirth']; 


$query ="INSERT INTO `slideshow`.`mymoduledb`(`first_name`,`last_name`,`year_of_birth`) VALUES ('{$firstName}','{$lastName}','{$yearofbirth}')"; 
    //$result=db_query($query); 

    if ($success = db_query($query)) { 
    // Tell the user that the employee has been saved. 
    drupal_set_message(t(' has been saved.')); 
    }else{ // If there's an error, $success will evaluate to FALSE, and the following code will execute. 
    drupal_set_message(t('There was an error saving your data. Please try again.')); 
     } 
    } 
} 

回答

0

中序編輯表單字段可以重用形式並設置默認值字段'#default_value' 存儲的形式提交使用相應的唯一身份ids.Refer的鏈接的示例代碼從數據庫中讀取的數據。

https://www.drupal.org/node/2069383

+0

先生,我很新的Drupal和PHP的我不理解上面的鏈接。有沒有可能發佈一個簡單的代碼來理解概念........謝謝.... – SQA

相關問題