Forums | Mahara Community

Developers /
Proper way to insert a record to a particular db table?

David Varney's profile picture
Posts: 34

04 August 2009, 2:28 PM

How do you insert a record into a table within the db?  I'm trying to insert a record into the blocktype_wall_post table and nothing shows up in the db table once the query is ran. 

Nigel's profile picture
Posts: 1643

04 August 2009, 6:45 PM

Hi - the insert_record() in lib/dml.php is what you want. There's plenty of examples of using it in the source, but as a quick example:

$record = new stdClass; 
$record->fieldname = 'fieldvalue';
$record->field2 = 'fieldvalue';
insert_record('tablename', $record); 

If you want the ID back:

$id = insert_record('tablename', $record, 'id', true); 
2 results