<p>Based on the information in the comments I'll provide an example how to save elements in Craft. I'll just show an example with an existing element type <code>Entry</code>. You can see how to create a custom element type well documented in the <a href="https://github.com/craftcms/docs/blob/v3/en/element-types.md" rel="nofollow noreferrer">docs</a> </p>

<p>You'll create a new <code>section</code> in your CP and add the required fields to the layout. In your case you'll need <code>title</code> (Booktitle), <code>author</code> (BookAuthor), <code>summary</code> (BookSummary) and <code>pages</code> (Pages)</p>

<pre><code>// get the section and the entrytype
// when you know your section id and entrytype id you don't
// need this. You can get the Ids by looking at the URL in the
// when you edit them
$section = Craft::$app-&gt;sections-&gt;getSectionByHandle('books');
$entryType = reset($section-&gt;getEntryTypes());
// Create an entry
$entry = new Entry([
    'sectionId' =&gt; $section-&gt;id,
    'typeId' =&gt; $entryType-&gt;id, // or insert the required type id manually
    'fieldLayoutId' =&gt; $entryType-&gt;fieldLayoutId,
    'authorId' =&gt; 1,
    'title' =&gt; 'A cool title of a book',
    'slug' =&gt; 'a-cool-title-of-a-book', // &lt;- not required
]);

$entry-&gt;setFieldValues([
    'author' =&gt; 'Me and Myself',
    'summary' =&gt; 'it was awesome',
    'pages' =&gt; 9000
]);

// Save the entry
Craft::$app-&gt;elements-&gt;saveElement($entry);

// in case there is something wrong..
$errors = $entry-&gt;getErrors();
</code></pre>

<p>When you are going to create your custom element type without a field layout your code could look like this</p>

<pre><code>$book = new Book([
    'title' =&gt; 'A cool title of a book',
    'author' =&gt; 'Me and Myself',
    'summary' =&gt; 'it was awesome',
    'pages' =&gt; 9000
]);
// Save the entry
Craft::$app-&gt;elements-&gt;saveElement($book);

// in case there is something wrong..
$errors = $book-&gt;getErrors();
</code></pre>

CC BY-SA 3.0
2018-04-04T21:18:07.773Z
2018-04-04T21:18:07.773Z
Robin Schambach
<p>I'm looking for a simple way to save form data to a database and be able to access that information. I'm not sure if I should use the custom fields or an entry (or a custom table) and if creating a module is the way to go or another easy way to have my form post submit handled with PHP.</p>

<p>I was hoping for a simple example of a module for Craft but I'm not finding it.</p>

<p>For the sake of an example, I'd like to add and edit books in my library:</p>

<pre><code>Book Type:
BookTitle - 50 characters
BookAuthor - 50 characters
BookSummary - 300 characters
Pages - Integer
</code></pre>

<p>Thanks.</p>

CC BY-SA 3.0
2018-04-03T19:18:07.517Z
Please read this and tell me if you would like to use `records` or `elements` to store your data. I'll show you the code depending on your choice/needs https://craftcms.stackexchange.com/questions/23859/database-structure-for-craft-3-to-import-from-external-source/23863#23863
Robin Schambach
CC BY-SA 3.0
2018-04-04T20:27:26.357Z
Seems like elements is the way to go since I believe the customers will need access to the table.  However, there's going to be quite a bit of coding anyways so we may go with records since everything will be customized.  Without seeing the difference, I would go with elements.
Aaron Jay Lev
<craft3><craft-features><craft-pro>
Looking for a simple way to add/update information records with CraftCMS
# Looking for a simple way to add/update information records with CraftCMS

(Asked by: username_0 on 2018-04-03)

I'm looking for a simple way to save form data to a database and be able to access that information. I'm not sure if I should use the custom fields or an entry (or a custom table) and if creating a module is the way to go or another easy way to have my form post submit handled with PHP.
I was hoping for a simple example of a module for Craft but I'm not finding it.
For the sake of an example, I'd like to add and edit books in my library:
Book Type:
BookTitle - 50 characters
BookAuthor - 50 characters
BookSummary - 300 characters
Pages - Integer

Thanks.

**Tags:** craft3, craft-features, craft-pro
> **Comments:**
> **username_1**: Please read this and tell me if you would like to use `records` or `elements` to store your data. I'll show you the code depending on your choice/needs https://craftcms.stackexchange.com/questions/23859/database-structure-for-craft-3-to-import-from-external-source/23863#23863  
> **username_0**: Seems like elements is the way to go since I believe the customers will need access to the table.  However, there's going to be quite a bit of coding anyways so we may go with records since everything will be customized.  Without seeing the difference, I would go with elements.  

---

## 1 answers

### Answer by username_1 on 2018-04-04. Score: 1

Based on the information in the comments I'll provide an example how to save elements in Craft. I'll just show an example with an existing element type Entry. You can see how to create a custom element type well documented in the docs 
You'll create a new section in your CP and add the required fields to the layout. In your case you'll need title (Booktitle), author (BookAuthor), summary (BookSummary) and pages (Pages)
// get the section and the entrytype
// when you know your section id and entrytype id you don't
// need this. You can get the Ids by looking at the URL in the
// when you edit them
$section = Craft::$app->sections->getSectionByHandle('books');
$entryType = reset($section->getEntryTypes());
// Create an entry
$entry = new Entry([
    'sectionId' => $section->id,
    'typeId' => $entryType->id, // or insert the required type id manually
    'fieldLayoutId' => $entryType->fieldLayoutId,
    'authorId' => 1,
    'title' => 'A cool title of a book',
    'slug' => 'a-cool-title-of-a-book', // <- not required
]);

$entry->setFieldValues([
    'author' => 'Me and Myself',
    'summary' => 'it was awesome',
    'pages' => 9000
]);

// Save the entry
Craft::$app->elements->saveElement($entry);

// in case there is something wrong..
$errors = $entry->getErrors();

When you are going to create your custom element type without a field layout your code could look like this
$book = new Book([
    'title' => 'A cool title of a book',
    'author' => 'Me and Myself',
    'summary' => 'it was awesome',
    'pages' => 9000
]);
// Save the entry
Craft::$app->elements->saveElement($book);

// in case there is something wrong..
$errors = $book->getErrors();


---

