Hai我希望将表单字段插入数据库并从moodle中的数据库中检索记录。怎么做:
这是我创建的形式:
Uplo.php
<?php
require_once('config.php');
require_once("$CFG->libdir/formslib.php");
class uplo extends moodleform {
public function definition() {
global $CFG;
$mform = $this->_form;
$buttonarray=array();
$mform->addElement('text', 'name', get_string('name'));
$mform->setType('name', PARAM_NOTAGS);
$mform->setDefault('name', 'Please enter name');
$mform->addElement('text', 'email', get_string('email'));
$mform->setType('email', PARAM_NOTAGS);
$mform->setDefault('email', 'Please enter email');
$buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
$buttonarray[] = &$mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
$mform->closeHeaderBefore('buttonar');
}
function validation($data, $files) {
return array();
}
}
?>
Testing.php:
<?php
define('_DB_HOST_NAME','localhost');
define('_DB_USER_NAME','root');
define('_DB_PASSWORD','');
define('_DB_DATABASE_NAME','moodle_test');
$dbConnection = mysqli_connect(_DB_HOST_NAME,_DB_USER_NAME,_DB_PASSWORD,_DB_DATABASE_NAME);
?>
<?php
require_once('uplo.php');
$mform = new uplo();
$mform->display();
if(isset($_POST['submitbutton'])){
$name = $mform->get_data('name');
$email = $mform->get_data('email');
$table='insert';
$res=insert_record($table, $name,$email, $returnid=true, $primarykey='id') ;
}
?>
这是我试过的。如何以正确的方式做到这一点
答案 0 :(得分:0)
确保你require_once(dirname( FILE )。&#39; /../../ config.php&#39;)(调整&#39; / ..的数量/&#39;基于您的文件所在的子目录,相对于config.php而言。
使用Moodle数据操作API https://docs.moodle.org/dev/Data_manipulation_API