2016-11-27 161 views
0

我在NEWMODULE插件和數據庫表中創建了一個簡單表單來保存表單數據,但是我不知道如何將表單數據插入到數據庫中。我保存代碼到my_form.php,這是我的代碼:如何在moodle中插入表單數據到數據庫中

class myform_form extends moodleform { 

public function definition() { 
    global $DB,$CFG; 

    $mform = &$this->_form; 
    $mform->addElement('text', 'tendethi', get_string('tendethi', 'myform')); 
    $mform->setType('tendethi', PARAM_TEXT); 
    $mform->setDefault('tendethi','Nhập tên đề thi'); 
    $mform->addElement('text', 'mdt', get_string('madethi','myform')); 
    $mform->setType('mdt', PARAM_TEXT); 
    $mform->setDefault('mdt','Nhập mã đề thi'); 
    $mform->addElement('duration', 'timelimit', get_string('thoigianlambai','myform')); 
    $mform->setType('timelimit', PARAM_TEXT); 
    $mform->addElement('text', 'gvrd', get_string('giaovienrade','myform')); 
    $mform->setType('gvrd', PARAM_TEXT); 
    $mform->setDefault('gvrd','Nhập họ tên giáo viên ra đề thi'); 
    $mform->addElement('text', 'tmh', get_string('tenmonhoc','myform')); 
    $mform->setType('tmh', PARAM_TEXT); 
    $mform->setDefault('tmh','Nhập tên môn học'); 

    $buttonarray=array(); 
    $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges')); 
    $buttonarray[] = &$mform->createElement('reset', 'resetbutton', get_string('revert')); 
    $buttonarray[] = &$mform->createElement('cancel'); 
    $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); 
    $mform->closeHeaderBefore('buttonar'); 

,這是代碼插入表單數據到數據庫:

// insert database into table 
//global $DB; 
$tendethi = optional_param('tendethi', null, PARAM_TEXT); 
$mdt = optional_param('mdt', null, PARAM_INT); 
$timelimit = optional_param('timelimit', null, PARAM_INT); 
$gvrd = optional_param('gvrd', null, PARAM_TEXT); 
$tmh = optional_param('tmh', null, PARAM_TEXT); 
if($data = $mform->get_data()){ 
$record = new stdClass(); 
$record->tendethi = $tendethi ; 
$record->mdt = $mdt ; 
$record->timelimit = $timelimit ; 
$record->gvrd = $gvrd ; 
$record->tmh = $tmh ; 
$DB=insert_record('myform_form', $record, false); 

}

請幫幫我!

回答

0

你的代碼看起來應該大致是這樣的:

$urlaftersubmission = new moodle_url(); // You will need to fill this in. 
$form = new myform_form(); 
if ($form->is_cancelled()) { 
    redirect($urlaftersubmission); 
} 
if ($data = $form->get_data()) { 
    $DB->insert_record('name_of_table', $data); 
    redirect($urlaftersubmission); 
} 
echo $OUTPUT->header(); 
$form->display(); 
echo $OUTPUT->footer(); 

注意,你可能要稍微調整一下數據,它是在正確的格式在數據庫中插入。您還應該確保使用插件中的install.xml或upgrade.php文件創建數據庫表(並使用Moodle內置的xmldb編輯器正確生成這些文件),否則Moodle將無法識別它們以插入數據。

+0

非常感謝你,我用xmldb製作了一張表來存儲數據,我試圖填寫我的表單並點擊保存按鈕,但是它的錯誤,這是一個錯誤: \ lib \ deprecatedlib.php的第857行:codepreception拋出 \ mod \ myform \ view.php的第56行:調用error()

Invalid array parameter detected in required_param(): timelimit
  • line 641 of \lib\moodlelib.php: call to debugging()
  • line 40 of \mod\myform\view.php: call to optional_param()

+0

我不清楚你爲什麼要調用optional_param - 如果你想從表單中獲取數據,調用get_data()(就像你已經在做了,正如我上面所示),沒有理由調用optional_param(特別是因爲它不會將'duration'字段格式化爲時間戳)。 – davosmith

+0

非常感謝你,我插入並選擇了數據庫。插入數據後,我會顯示信息表數據已插入,我想在此表中創建編輯和刪除按鈕,旁邊的表中的數據行,我應該怎麼做?你可以幫我嗎?非常感謝你。 –

相關問題