General developer forum

Create custom page

Picture of Mateusz Dbrznsk
Create custom page

Hello Moodle Comunity!

I have got a weird problem with my custom moodle page. I moodle database I created a new table with three fields:
- user_id (as string),
- agreement (boolean),
- agreement_date (for development as string).

Also, I create a custom PHP page, where when, the user enters on the page this will be annotated in DB.

I create the query using moodle docs:

$myData= new stdClass();
$myData->user_id = getCurrentUserFunction();
$myData->agreement = 'true';
$myData->agreement_date= getTodayDateFunction();

$DB->insert_record('my_table', $myData); 

When thic code is running I get a information "Error writing to database", but... my data is saved in the DB.

Maybe somebody has some idea?

Best regards,

Average of ratings: -
Picture of Mark Johnson
Re: Create custom page
Core developersParticularly helpful MoodlersPlugin developers
$DB->insert_record will normally return the ID of the new record, using the auto-incrementing id field in your table.  It looks like you haven't included a field like this in your table, so I'm guessing that you're seeing the error as it can't get this ID value.

When adding or altering moodle database tables for your plugin, you should always use the XMLDB editor to create an install.xml file.  This will automatically add the required id field to your tables, as well as preventing other things that might cause errors when interacting with the database API.  It will also generate the PHP code you need for you upgrade scripts if you make changes to the database in the future.
Average of ratings: -