Question on insert_record() function

Question on insert_record() function

by Tim Redman -
Number of replies: 11
I'm fairly new to Moodle and PHP programming in general and I'm trying to get my head around the functions provided in datalib.php.

What I am hoping to do is create a form and take the values and submit them into a table I have created. Even more basic than that at the moment I'm trying to simply create a php page that will submit a record into a manually created table.

I think insert_record() is the function I should be using and I have included datalib.php and config.php in the php script but do not seem to be able to get it to work.

<?php
require_once("config.php");
require_once("datalib.php");

$newentry->test = array('2', 'test');
$table = 'mdl_test_table';

insert_record($table, $newentry)
?>

As you can see, I really am a beginner at all this. Any suggestions as to what I'm doing wrong or suggestions for blocks or activity modules that include submitting values to a database table I can look at for clues would be great.

Any help much appreciated, would be nice to get to grips with all this and hopefully eventually contribute something usefull back to the moodle community!

Thanks
Tim
Average of ratings: -
In reply to Tim Redman

Re: Question on insert_record() function

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
First, go to the admin page on your test set up, and turn on the various debug options. Then you will get error messages when you screw up (which we all do wink)

In your first require. Don't you need to specify a path?

If the first include works, you won't need the second one.

Then to create the newentry, you actually need to do

$newentry = new stdClass;
$newentry->firstcolumnname = 2;
$newentry->secondcolumnname = 'test';
insert_record('test_table', $newentry)

Try that and let us know how you get on.
In reply to Tim Redman

Re: Question on insert_record() function

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
In addition to Tim (the other one)'s comments, you should ensure that your table has a column called 'id' that is an autoincrementing integer. This is sometimes required for the _record functions. Also see the Moodle coding guidelines on the wiki.

--sam
In reply to sam marshall

Re: Question on insert_record() function

by Tim Redman -
Tim and Sam, after a couple of issues this afternoon I've finally got it working! I can now insert static content in my new table. Your posts were super helpfull though so thank you!

Now I just have to figure out how to use this to take input from a form and put it into the db but this certainly seems like a step in the right direction! Thanks again for the help and i'll certainly check out the wiki!





In reply to Tim Redman

Re: Question on insert_record() function

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Have a look at /admin/timezone.php

At the bottom, it outputs a form where the timezone can be selected.

At the top, it calls optional_param to see if any data was submitted. It it was, it does some processing before displaying the form again.

That is very typical.

Anyway, to get the input, you need to use the optional_param/required_param functions that live in moodlelib.php.
In reply to Tim Hunt

Re: Question on insert_record() function

by Tim Redman -
Thanks again Tim! That was just what I was looking for and now having a working form inputing data using the moodle functions. These moodle functions are proving very usefull!

There is just one last thing I need to figure out to complete this mini learning project. That is that I need to identify which moodle user is filling out the form so I can retrieve that users data later on. I have a field in my db called userid that i'm hoping to populate with the user id of the logged in moodle user. Is there a function that returns the user id that I can use to submit that id to the database?

 
In reply to Tim Redman

Re: Question on insert_record() function

by Greg Lyon -
Tim, try $USER->id

once you've done the require_once() there are a wealth of parameters available to you in Moodle.  $USER is just one of them.  ($CFG, $COURSE would be others). 

When you're learning it's quite useful to see what is contained in some of these variables.  If you haven't done this yet try (in a php page):
I also included it as a file with pretty formatting and a couple other variables.  Really helps getting you used to moodle development.

In reply to Greg Lyon

Re: Question on insert_record() function

by Tim Redman -
Thanks Greg I'd just about managed to figure out using $USER->id was the way to do it. That file you attached is excellent, I can see that coming in really handy as I continue to get to grips with moodle development, thanks! 
In reply to Tim Redman

Re: Question on insert_record() function

by Greg Lyon -
From one newbie to another wink Happy to help!  
In reply to Greg Lyon

Re: Question on insert_record() function

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
Yup, this is very common way to debug Moodle code. I often use

print_object($something); die(); // DONOTCOMMIT

So often that I have a ViM macro to produce such line. Note the comment I use there. Then I can easily check (grep) I have no forgotten trace point in the code.
Average of ratings: Useful (1)