Answers.Body: <p>If you really, really want to create an entry from within the database (you must have a VERY good reason to do so), then you should create a row in:</p>
  
  <ul>
  <li>entries</li>
  <li>elements</li>
  <li>elements_sites</li>
  <li>content</li>
  </ul>
  
  <p>If you'd like the easier path, use <a href="https://docs.craftcms.com/v3/extend/migrations.html#creating-migrations" rel="nofollow noreferrer">Content migrations</a>. Create a migration, and then add something like:</p>
  
  <pre><code>// Create the entry
  $myNewEntry = new craft\elements\Entry();
  $myNewEntry-&gt;setFieldValues([
      'title' =&gt; 'My new entry',
      'myCustomFieldHandle' =&gt; 'Look, I can even set a custom field!',
      'myCustomCategoryFieldHandle' =&gt; [$categoryILookedUp-&gt;id],
  ]);
  
  // Save the entry. Everything else will be handled for you by Craft's API.
  Craft::$app-&gt;getElements()-&gt;saveElement($myNewEntry);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-09T22:12:01.013Z
Answers.Text: The Craft 2 Content migration docs: https://docs.craftcms.com/v2/plugins/migrations.html
Answers.UserDisplayName: ponies
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-14T22:24:17.113Z
Answers.LastActivityDate: 2020-02-14T22:24:17.113Z
Answers.UserDisplayName: Paul
Body: <p>I'm trying to create a new entry from within the database. The section is defined in craft_sections, and the entries for craft_elements, and craft_entries seem correct after the script is run, but no entries are displayed in the UI.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-12T20:56:54.917Z
Comments.Text: I'd suggest not doing it this way, and instead go through the Craft APIs for building out the Craft data structures.
Comments.UserDisplayName: andrew.welch
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-12T21:03:15.773Z
Comments.Text: I concur, and have shared your recommendation.
Comments.UserDisplayName: ponies
Tags: <database><craft2>
Title: How to create an entry with MySQL?
ThreadText: # How to create an entry with MySQL?
  
  (Asked by: username_0 on 2020-02-12)
  
  I'm trying to create a new entry from within the database. The section is defined in craft\_sections, and the entries for craft\_elements, and craft\_entries seem correct after the script is run, but no entries are displayed in the UI.
  
  **Tags:** database, craft2
  
  > **Comments:**
  > **username_1**: I'd suggest not doing it this way, and instead go through the Craft APIs for building out the Craft data structures.  
  > **username_0**: I concur, and have shared your recommendation.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-02-14. Score: 3
  
  If you really, really want to create an entry from within the database (you must have a VERY good reason to do so), then you should create a row in:
  
  * entries
  * elements
  * elements\_sites
  * content
  
  If you'd like the easier path, use [Content migrations](https://docs.craftcms.com/v3/extend/migrations.html#creating-migrations). Create a migration, and then add something like:
  
  ```
  // Create the entry
  $myNewEntry = new craft\elements\Entry();
  $myNewEntry->setFieldValues([
      'title' => 'My new entry',
      'myCustomFieldHandle' => 'Look, I can even set a custom field!',
      'myCustomCategoryFieldHandle' => [$categoryILookedUp->id],
  ]);
  
  // Save the entry. Everything else will be handled for you by Craft's API.
  Craft::$app->getElements()->saveElement($myNewEntry);
  
  ```
  
  > **Comments:**
  > **username_0**: The Craft 2 Content migration docs: https://docs.craftcms.com/v2/plugins/migrations.html  
  
  ---
  
  