本地MAMP环境下WordPress无法编辑functions.php及页面求助
问题:WordPress后台编辑文件/页面报错及数据库插入失败
刚安装WordPress 6.4.3和MAMP,最初成功编辑并保存了Twenty Twenty-Four主题的functions.php文件,但后续再次修改该文件时,WordPress提示:
出现错误,您的更改可能未保存,请重试。您可能需要通过FTP手动修复并上传文件。
同时编辑网站页面时也出现报错:
更新失败,响应不是有效的JSON响应。
以下是添加到functions.php后导致无法再编辑的代码:
if ($_SERVER["REQUEST_METHOD"] === "POST") { $name = sanitize_text_field($_POST["name"]); $email = sanitize_email($_POST["email"]); $message = sanitize_textarea_field($_POST["description"]); // 将表单数据保存到数据库 global $wpdb; $table_name = $wpdb->prefix . 'request_form'; $data = array( 'name' => $name, 'email' => $email, 'description' => $message, 'submission_time' => current_time('mysql') ); $insert_result = $wpdb->insert($table_name, $data); if ($insert_result === false) { $response = array( 'success' => false, 'message' => 'Error saving the form data.', ); } else { $response = array( 'success' => true, 'message' => 'Form data saved successfully.' ); } // 返回JSON响应 header('Content-Type: application/json'); echo json_encode($response); exit; }
已尝试的操作:
- 检查文件权限,为本地所有用户赋予读写权限;
- 重启服务器;
- 将WordPress移动到新文件夹;
- 在「设置>常规」中把WordPress地址和站点地址从http改为https并保存后,被重定向到
http://localhost:8888/wp-admin/options.php,出现以下报错:
提示: 未定义索引: name,位于 /Applications/MAMP/htdocs/wp-content/themes/twentytwentyfour/functions.php 第 209行
提示: 未定义索引: email,位于 /Applications/MAMP/htdocs/wp-content/themes/twentytwentyfour/functions.php 第 210行
提示: 未定义索引: description,位于 /Applications/MAMP/htdocs/wp-content/themes/twentytwentyfour/functions.php 第 211行
WordPress数据库错误: [字段列表中不存在'submission_time'列]INSERT INTO `wp_request_form` (`name`, `email`, `description`, `submission_time`) VALUES ('', '', '', '2024-03-30 07:10:51'){"success":false,"message":"表单数据保存失败。"}
内容的提问来源于stack exchange,提问作者Emer
相关产品推荐
相关产品推荐

