How to insert multi select records?

Re: How to insert multi select records?

by Chris Wharton -
Number of replies: 0
Hi Mian

The form data is return as an object:
object(stdClass)[1204]
  public 'courseid' => 
    array (size=2)
      0 =>  '3' (length=1)
      1 =>  '2' (length=1)
  public 'planid' =>  '0' (length=1)
  public 'username' =>  'admin' (length=5)
  public 'submitbutton' =>  'submit' (length=6)
The selected courses are in an array, which can't be directly inserted into the database. You need to process this data first.
This is often done with an explicitly created object for insertion into the database. For example:
$todb = new stdClass();
$todb->courses = implode(',', $formdata->courseid);
$todb->username = $formdata->username;
$DB->insert_record('table', $todb);
There are examples of this throughout core Moodle code, which you should examine to figure out ways to improve your code.