Databases: Problem with insert_record()

Databases: Problem with insert_record()

Marty Borisov發表於
Number of replies: 4

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 dataResult from print_object() (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. 

評比平均分數: -
In reply to Marty Borisov

Re: Databases: Problem with insert_record()

Marty Borisov發表於

Hi again,

I have found what I had missed. To me more correct what I hadn't understood. I had only created the table using the XMLDB editor and I had thought that there had been a created table in the moodle DB, but unfortunately I hadn't understood right the using of the XMLDB editor. I checked and there was not any table called 'block_simplehtml'. I created it via phpMyAdmin and now I am able to insert records in that table.

In reply to Marty Borisov

Re: Databases: Problem with insert_record()

james mergenthaler發表於

Marty, glad you figured things out.  Use the moodle error logs for good detailed errors.  First, set the debugging option to robust in the administator then locate the moodle error log, look in php.ini for error_log setting, mine looks like this;

error_log = "c:/wamp/logs/php_error.log"

Then, open the error log which probably would have alerted you sooner to the problem.

regards

In reply to Marty Borisov

Re: Databases: Problem with insert_record()

Rafiq Muhammad發表於

Hi Marty,

Just a point of note. It is not advisable to create your table directly using phpMyAdmin after you have edited the XMLDB. The proper way is to use the database update feature that comes with every module. Please see http://docs.moodle.org/dev/Installing_and_upgrading_plugin_database_tables
 for more information.

May be you already figure this out, but I am putting this in the forum for future reference when some other who may encounter the same problem may stumble into this page.

Cheers