Hi,
I'm having a problem with inserting data in table. So, I create my own form (I'm just following the tutorial from moodle programming) in a block (used in the courses) and I have 2 buttons: Save and Cancel. My form appears correctly,
I'm able to enter data. I can see what I enter using the function print_object() and everything looks OK and I have the data (the object is not empty).
if ( $fromform = $simplehtml->get_data() ) {
print_object($fromform);
}
So, the next step is to use a function to insert the data in the database and to get back to the course. I use in the body of the IF condition
if(!$DB->insert_record('block_simplehtml',$fromform)){
print_error(get_string('inserterror' , 'block_simplehtml'));
}
redirect($CFG->wwwroot.'/course/view.php?id='.$courseid); instead of the funtion print_object($fromform).
But an error appears. Here is the trace:
Stack trace:
- line 878 of \lib\dml\mysqli_native_moodle_database.php: coding_exception thrown
- line 935 of \lib\dml\mysqli_native_moodle_database.php: call to mysqli_native_moodle_database->insert_record_raw()
- line 43 of \blocks\simplehtml\page.php: call to mysqli_native_moodle_database->insert_record()
And the error: Coding error detected, it must be fixed by a programmer: moodle_database::insert_record_raw() no fields found.
I looked at the file \lib\dml\mysqli_native_moodle_database.php and I found out that this error is for an empty passed object:
if (empty($params)) {
throw new coding_exception('moodle_database::insert_record_raw() no fields found.');
}
I also tried an elementary way to check whether the object is empty using the function above in my code: if(!empty($fromform)) { echo "NOT EMPTY"; } and it was written on the screen.
I can't find out where I'm losing my data and why the object is empty when I call insert_record(). Could someone please explain me where I'm making a mistake. Thank you.