Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-23T12:40:58.893Z
Answers.Text: Let me know if you got it
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-23T12:58:51.630Z
Answers.Text: ok i will notify
Answers.UserDisplayName: zain
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-23T08:00:54.693Z
Answers.LastActivityDate: 2018-02-23T08:00:54.693Z
Answers.UserDisplayName: Robin Schambach
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-30T09:34:44.273Z
Answers.LastActivityDate: 2020-03-30T09:34:44.273Z
Answers.UserDisplayName: jamesSeb
Body: <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>
  
Tags: <database>
Title: Simple save data to db in Craft CMS
ThreadText: # 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](http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder)
  
  ```
  craft()->db->createCommand()
      ->insert('mytablename', array(
          'field1'    => 'foo',
          'field2'    => 'bar'
      ));
  
  ```
  
  Or you can use [Active Records](http://www.yiiframework.com/doc/guide/1.1/en/database.ar) in [Craft](https://craftcms.com/docs/plugins/records)
  
  ```
  $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();
  
  ```
  
  
  ---
  
  