I am new to Craft CMS. Please tell me about how can I save data into database in Craft?

I have searched a lot of results about my query but nothing give advantage to me.

Thanks in advance.


I suggest you to read about the Yii Query Builder

    ->insert('mytablename', array(
        'field1'    => 'foo',
        'field2'    => 'bar'

Or you can use Active Records in Craft

$record = new Record();
$record->field1 = 'foo';
$record->field2 = 'bar';

Or you can use Craft Elements (entries/categories/tags)

$entry = new EntryModel();
$attributes = [
   'section'   => $yourSectionId,
   'entryType' => $yourEntryTypeId,
   'field1'    => 'foo',
   'field2'    => 'bar'
  • Let me know if you got it Feb 23 '18 at 12:40
  • ok i will notify
    – zain
    Feb 23 '18 at 12:58

there should be execute() fn at the last to execute the sql query

->insert('mytablename', array(
    'field1'    => 'foo',
    'field2'    => 'bar'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.