<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>

CC BY-SA 4.0
2020-03-09T22:12:01.013Z
The Craft 2 Content migration docs: https://docs.craftcms.com/v2/plugins/migrations.html
ponies
CC BY-SA 4.0
2020-02-14T22:24:17.113Z
2020-02-14T22:24:17.113Z
Paul
<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>

CC BY-SA 4.0
2020-02-12T20:56:54.917Z
I'd suggest not doing it this way, and instead go through the Craft APIs for building out the Craft data structures.
andrew.welch
CC BY-SA 4.0
2020-02-12T21:03:15.773Z
I concur, and have shared your recommendation.
ponies
<database><craft2>
How to create an entry with MySQL?
# 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. 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  

---

