WordPress error “Could not insert attachment into the database” when uploading files

Solution:

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.