Can't save images
edalzell opened this issue · 5 comments
edalzell commented
I have a form that is supposed to save images (assets):
<label for="images">Add images</label>
<input type="file" id="images" name="fields[images][]" multiple><br>
{% if entry is defined %}
{{ errorList(entry.getErrors('mailing')) }}
{% endif %}
When I choose a file and submit, nothing is in images
for that entry.
What am I doing wrong?
edalzell commented
Current version of Guest Entries
edalzell commented
wallacio commented
The very same for me, unfortunately.
Guest Entries: 2.3.0
Craft Pro 3.4.24
I have two fields which are of type Asset.
Form fields of the format:
<input type="file" name="fields[participantRouteImage][]" id="participantRouteImage" class="custom-file-input">
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`databasename`.`relations`, CONSTRAINT `relations_targetId_fk` FOREIGN KEY (`targetId`) REFERENCES `elements` (`id`) ON DELETE CASCADE)
The SQL being executed was: INSERT INTO `relations` (`fieldId`, `sourceId`, `sourceSiteId`, `targetId`, `sortOrder`, `dateCreated`, `dateUpdated`, `uid`) VALUES (617, 50112, NULL, 20190629, 1, '2020-06-18 15:30:38', '2020-06-18 15:30:38', '333792e8-7809-4a10-9bc4-49cc204914e2')
Stack trace:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`databasename`.`relations`, CONSTRAINT `relations_targetId_fk` FOREIGN KEY (`targetId`) REFERENCES `elements` (`id`) ON DELETE CASCADE) in .../application/vendor/yiisoft/yii2/db/Command.php:1293
Stack trace:
#0 .../application/vendor/yiisoft/yii2/db/Command.php(1293): PDOStatement->execute()
#1 .../application/vendor/yiisoft/yii2/db/Command.php(1093): yii\db\Command->internalExecute('INSERT INTO `re...')
#2 .../application/vendor/craftcms/cms/src/services/Relations.php(110): yii\db\Command->execute()
#3 .../application/vendor/craftcms/cms/src/fields/BaseRelationField.php(746): craft\services\Relations->saveRelations(Object(craft\fields\Assets), Object(craft\elements\Entry), Array)
#4 .../application/vendor/craftcms/cms/src/fields/Assets.php(546): craft\fields\BaseRelationField->afterElementSave(Object(craft\elements\Entry), true)
#5 .../application/vendor/craftcms/cms/src/base/Element.php(2559): craft\fields\Assets->afterElementSave(Object(craft\elements\Entry), true)
#6 .../application/vendor/craftcms/cms/src/elements/Entry.php(1301): craft\base\Element->afterSave(true)
#7 .../application/vendor/craftcms/cms/src/services/Elements.php(2166): craft\elements\Entry->afterSave(true)
#8 .../application/vendor/craftcms/cms/src/services/Elements.php(531): craft\services\Elements->_saveElementInternal(Object(craft\elements\Entry), true, false, true)
#9 .../application/vendor/craftcms/guest-entries/src/controllers/SaveController.php(137): craft\services\Elements->saveElement(Object(craft\elements\Entry))
#10 [internal function]: craft\guestentries\controllers\SaveController->actionIndex()
#11 .../application/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#12 .../application/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#13 .../application/vendor/craftcms/cms/src/web/Controller.php(178): yii\base\Controller->runAction('', Array)
#14 .../application/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('', Array)
#15 .../application/vendor/craftcms/cms/src/web/Application.php(291): yii\base\Module->runAction('guest-entries/s...', Array)
#16 .../application/vendor/craftcms/cms/src/web/Application.php(559): craft\web\Application->runAction('guest-entries/s...', Array)
#17 .../application/vendor/craftcms/cms/src/web/Application.php(270): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#18 .../application/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#19 .../application/web/index.php(21): yii\base\Application->run()
#20 {main}
Next yii\db\IntegrityException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`databasename`.`relations`, CONSTRAINT `relations_targetId_fk` FOREIGN KEY (`targetId`) REFERENCES `elements` (`id`) ON DELETE CASCADE)
The SQL being executed was: INSERT INTO `relations` (`fieldId`, `sourceId`, `sourceSiteId`, `targetId`, `sortOrder`, `dateCreated`, `dateUpdated`, `uid`) VALUES (617, 50112, NULL, 20190629, 1, '2020-06-18 15:30:38', '2020-06-18 15:30:38', '333792e8-7809-4a10-9bc4-49cc204914e2') in .../application/vendor/yiisoft/yii2/db/Schema.php:674
Stack trace:
#0 .../application/vendor/yiisoft/yii2/db/Command.php(1298): yii\db\Schema->convertException(Object(PDOException), 'INSERT INTO `re...')
#1 .../application/vendor/yiisoft/yii2/db/Command.php(1093): yii\db\Command->internalExecute('INSERT INTO `re...')
#2 .../application/vendor/craftcms/cms/src/services/Relations.php(110): yii\db\Command->execute()
#3 .../application/vendor/craftcms/cms/src/fields/BaseRelationField.php(746): craft\services\Relations->saveRelations(Object(craft\fields\Assets), Object(craft\elements\Entry), Array)
#4 .../application/vendor/craftcms/cms/src/fields/Assets.php(546): craft\fields\BaseRelationField->afterElementSave(Object(craft\elements\Entry), true)
#5 .../application/vendor/craftcms/cms/src/base/Element.php(2559): craft\fields\Assets->afterElementSave(Object(craft\elements\Entry), true)
#6 .../application/vendor/craftcms/cms/src/elements/Entry.php(1301): craft\base\Element->afterSave(true)
#7 .../application/vendor/craftcms/cms/src/services/Elements.php(2166): craft\elements\Entry->afterSave(true)
#8 .../application/vendor/craftcms/cms/src/services/Elements.php(531): craft\services\Elements->_saveElementInternal(Object(craft\elements\Entry), true, false, true)
#9 .../application/vendor/craftcms/guest-entries/src/controllers/SaveController.php(137): craft\services\Elements->saveElement(Object(craft\elements\Entry))
#10 [internal function]: craft\guestentries\controllers\SaveController->actionIndex()
#11 .../application/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#12 .../application/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#13 .../application/vendor/craftcms/cms/src/web/Controller.php(178): yii\base\Controller->runAction('', Array)
#14 .../application/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('', Array)
#15 .../application/vendor/craftcms/cms/src/web/Application.php(291): yii\base\Module->runAction('guest-entries/s...', Array)
#16 .../application/vendor/craftcms/cms/src/web/Application.php(559): craft\web\Application->runAction('guest-entries/s...', Array)
#17 .../application/vendor/craftcms/cms/src/web/Application.php(270): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#18 .../application/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#19 .../application/web/index.php(21): yii\base\Application->run()
#20 {main}
Additional Information:
Array
(
[0] => 23000
[1] => 1452
[2] => Cannot add or update a child row: a foreign key constraint fails (`databasename`.`relations`, CONSTRAINT `relations_targetId_fk` FOREIGN KEY (`targetId`) REFERENCES `elements` (`id`) ON DELETE CASCADE)
)
Submits fine if no image selected.
edalzell commented
I solved this @wallacio, I didn't have the enctype=multipart/form-data
set on the form tag, that caused that integrity constraint
wallacio commented
Haha - I just found this out too, was about to post my solution!!