<p>I suggest you to read about the <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder" rel="nofollow noreferrer">Yii Query Builder</a> </p>

<pre><code>craft()-&gt;db-&gt;createCommand()
    -&gt;insert('mytablename', array(
        'field1'    =&gt; 'foo',
        'field2'    =&gt; 'bar'
    ));
</code></pre>

<p>Or you can use <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.ar" rel="nofollow noreferrer">Active Records</a> in <a href="https://craftcms.com/docs/plugins/records" rel="nofollow noreferrer">Craft</a></p>

<pre><code>$record = new Record();
$record-&gt;field1 = 'foo';
$record-&gt;field2 = 'bar';
$record-&gt;save();
</code></pre>

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

<pre><code>$entry = new EntryModel();
$attributes = [
   'section'   =&gt; $yourSectionId,
   'entryType' =&gt; $yourEntryTypeId,
   'field1'    =&gt; 'foo',
   'field2'    =&gt; 'bar'
];
$entry-&gt;getContent()-&gt;setAttributes($attributes);
craft()-&gt;entries()-&gt;saveEntry($entry); 
</code></pre>

CC BY-SA 3.0
2018-02-23T12:40:58.893Z
Let me know if you got it
Robin Schambach
CC BY-SA 3.0
2018-02-23T12:58:51.630Z
ok i will notify
zain
CC BY-SA 3.0
2018-02-23T08:00:54.693Z
2018-02-23T08:00:54.693Z
Robin Schambach
<p>there should be execute() fn at the last to execute the sql query</p>

<pre><code>craft()-&gt;db-&gt;createCommand()
-&gt;insert('mytablename', array(
    'field1'    =&gt; 'foo',
    'field2'    =&gt; 'bar'
))
-&gt;execute();
</code></pre>

CC BY-SA 4.0
2020-03-30T09:34:44.273Z
2020-03-30T09:34:44.273Z
jamesSeb
<p>I am new to Craft CMS.  Please tell me about how can I save data into database in Craft? </p>

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

<p>Thanks in advance.</p>

<database>
Simple save data to db in Craft CMS
# Simple save data to db in Craft CMS

(Asked by: username_0 on 2018-02-23)

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.

**Tags:** database

---

## 2 answers

### Answer by username_1 on 2018-02-23. Score: 4

I suggest you to read about the Yii Query Builder 
craft()->db->createCommand()
    ->insert('mytablename', array(
        'field1'    => 'foo',
        'field2'    => 'bar'
    ));

Or you can use Active Records in Craft
$record = new Record();
$record->field1 = 'foo';
$record->field2 = 'bar';
$record->save();

Or you can use Craft Elements (entries/categories/tags)
$entry = new EntryModel();
$attributes = [
   'section'   => $yourSectionId,
   'entryType' => $yourEntryTypeId,
   'field1'    => 'foo',
   'field2'    => 'bar'
];
$entry->getContent()->setAttributes($attributes);
craft()->entries()->saveEntry($entry);

> **Comments:**
> **username_1**: Let me know if you got it  
> **username_0**: ok i will notify  

---

### Answer by username_2 on 2020-03-30. Score: 1

there should be execute() fn at the last to execute the sql query
craft()->db->createCommand()
->insert('mytablename', array(
    'field1'    => 'foo',
    'field2'    => 'bar'
))
->execute();


---

