使用数据操作API-Moodle插入数据库

时间:2015-06-30 11:13:06

标签: php database moodle

当用户点击“保存”按钮时,我正在尝试将数据插入到我的数据库中,但是我收到此错误消息:

  

缺少必需的参数(名称)

     

有关此错误的详细信息

Debug info: 
Error code: missingparam
Stack trace:
line 463 of \lib\setuplib.php: moodle_exception thrown
line 548 of \lib\moodlelib.php: call to print_error()
line 8 of \local\try\process.php: call to required_param()

以下是表格中代码的一部分:

<?php
$table = new html_table();
$table->data[] = array("Faculty ID", "<input type='text' name='facid' size='60'");
$table->data[] = array("Faculty Name", "<input type='text' name='name' size='60'");
$table->data[] = array("Comment", "<input type='text' name='comment' size='60'");

echo html_writer::table($table);
?>
<div style="text-align:center;">
    <input type='button' onClick="location='index.php'" value="Back"/>&nbsp; <input type='button' value='Save' onClick="location='process.php'"/>
</div>

process.php

<?php

require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');

global $DB;

$id = optional_param('facid', null, PARAM_TEXT);
$name = required_param('name', PARAM_TEXT);
$comment = required_param('comment', PARAM_TEXT);

$record1 = new stdClass();
$record1->name  = $id;
$record1->displayorder = '10000';

$record2 = new stdClass();
$record2->name  = $name;
$record2->displayorder = '10000';

$record3 = new stdClass();
$record3->name = $comment;
$record3->displayorder = '10000';

// Insert one record at a time.
$lastinsertid1 = $DB->insert_record('faculty', $record1);
$lastinsertid2 = $DB->insert_record('faculty', $record2);
$lastinsertid3 = $DB->insert_record('faculty', $record3);

if(!$lastinsertid)
{
    echo "Could not insert";
}
else
{
    echo "Successful";
}

?>

知道为什么我会收到这个错误吗?感谢。

1 个答案:

答案 0 :(得分:1)

你的保存&#39;按钮不会提交表单中的任何数据,只是将浏览器的网址更改为“&gt; process.php&#39;。

如果要处理数据,则需要将其全部包装在表单中,并添加类型为&#39; submit&#39;的输入。到最后(尽管如果你正在使用Moodle,你应该使用Moodle Form API)。