<p>Problem solved! (head slap) </p>

<p>The <code>&lt;form&gt;</code> requires the following parameters:<br>
<code>enctype="multipart/form-data"</code></p>

<p>An odd error that gets thrown if omitted. </p>

CC BY-SA 4.0
2019-04-26T10:39:00.743Z
Same here, <form enctype="multipart/form-data"> fixed the issue.
Avant la lettre
CC BY-SA 4.0
2014-09-04T00:55:57.963Z
2019-05-02T17:31:23.743Z
2019-05-02T17:31:23.743Z
Derrick Grigg
<p>I am trying to upload/add an asset to an entry via a form on the website. The entry saves fine when I don't include the asset field but as soon as I add the asset field and try to upload a file I get this odd SQL error.</p>

<blockquote>
  <p>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (<code>housewares</code>.<code>craft_relations</code>, CONSTRAINT <code>craft_relations_targetId_fk</code> FOREIGN KEY (<code>targetId</code>) REFERENCES <code>craft_elements</code> (<code>id</code>) ON DELETE CASCADE).<br>
  The SQL statement executed was: INSERT INTO <code>craft_relations</code> (<code>fieldId</code>, <code>sourceId</code>, <code>sourceLocale</code>, <code>targetId</code>, <code>sortOrder</code>, <code>dateCreated</code>, <code>dateUpdated</code>, <code>uid</code>) VALUES (:row0_col0, :row0_col1, NULL, :row0_col3, :row0_col4, :row0_col5, :row0_col6, :row0_col7). Bound with :row0_col0='4', :row0_col1='27', :row0_col3='Golfclubs.jpg', :row0_col4=1, :row0_col5='2014-09-03 23:36:06', :row0_col6='2014-09-03 23:36:06', :row0_col7='a1e565e1-f6d3-4f3b-8a6f-2c049d236b13'</p>
</blockquote>

<p>The field works fine in the CP.</p>

<p>This is the field</p>

<pre><code>&lt;input class="form-control" type="file" id="image" name="fields[image][]" /&gt;
</code></pre>

<p>Any ideas?</p>

<entries><assets><form><guest-entries>
Upload an asset in a custom form
# Upload an asset in a custom form

(Asked by: username_0 on 2014-09-03)

I am trying to upload/add an asset to an entry via a form on the website. The entry saves fine when I don't include the asset field but as soon as I add the asset field and try to upload a file I get this odd SQL error.

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (housewares.craft_relations, CONSTRAINT craft_relations_targetId_fk FOREIGN KEY (targetId) REFERENCES craft_elements (id) ON DELETE CASCADE).
  The SQL statement executed was: INSERT INTO craft_relations (fieldId, sourceId, sourceLocale, targetId, sortOrder, dateCreated, dateUpdated, uid) VALUES (:row0_col0, :row0_col1, NULL, :row0_col3, :row0_col4, :row0_col5, :row0_col6, :row0_col7). Bound with :row0_col0='4', :row0_col1='27', :row0_col3='Golfclubs.jpg', :row0_col4=1, :row0_col5='2014-09-03 23:36:06', :row0_col6='2014-09-03 23:36:06', :row0_col7='a1e565e1-f6d3-4f3b-8a6f-2c049d236b13'

The field works fine in the CP.
This is the field


Any ideas?

**Tags:** entries, assets, form, guest-entries


---

## 1 answers

### Answer by username_0 on 2014-09-04. Score: 3

Problem solved! (head slap)

The  requires the following parameters:  
`enctype="multipart/form-data"`

An odd error that gets thrown if omitted.

> **Comments:**
> **username_1**: Same here, <form enctype="multipart/form-data"> fixed the issue.  

---

