Wordpress error "Could not insert attachment into the database" when uploading files

Viewed 4685

After working on the website without issue for the afternoon, I am suddenly presented with a "Could not insert attachment into the database." error any time I try to upload any files. The size and type of file does not seem to matter.

I have checked permissions and the file upload size and memory limits in WordPress and don't see an issue there. No changes were made between the last successful upload and uploads failing (only a matter of minutes).

I have found that the files are uploading correctly to the file system but are not available within WordPress.

1 Answers

This is likely a MySQL database issue. To find the exact error that's preventing you from uploading media, you can turn on wordpress debug logging by adding define('WP_DEBUG_LOG', true); to your wp-config.php file, then re-create the error, and find the full error message in the debug.log file inside wp-content folder (more detailed instructions can be found here).

In my case, the error was a duplicated primary key when trying to insert into the mac_posts table. The images were actually uploading to the wp-content/uploads folder on the server just fine, but wordpress makes an entry in the mac_posts table for every media item uploaded so it can display it in the wordpress media library. Looking in the database table using phpMyAdmin, the row with the existing ID was a wpforms contact form, so I just deleted that row from the table, re-created the contact form manually, and that fixed the table and the upload issue.

Related