提问人:newbie 提问时间:10/5/2023 最后编辑:newbie 更新时间:10/5/2023 访问量:19
尝试通过php代码片段将数据更新到WordPress中的数据库,为什么数据库中的数据没有更新并显示404错误?
Trying to update data to database in WordPress by php code snippets , why the data is not updating in database and showing 404 error?
问:
尝试更新mysql数据库(不是本地服务器)中的数据。使用基本的PHP和jQuery,但数据未更新,并在检查部分显示404错误状态。我的代码有什么问题?
jQuery(document).ready(function ($) {
// Handle update button click
$(".update-btn").on("click", function () {
var id = $(this).data("id");
var name = $("td[data-id='" + id + "'][data-field='name']").text();
var description = $("td[data-id='" + id + "'][data-field='description']").text();
// Send AJAX request to update data (custom post content)
$.ajax({
type: "POST",
url: window.location.href,
data: {
action: "update",
id: id,
name: name,
description: description
},
success: function (response) {
// Handle success, if needed
}
});
});
elseif ($action === 'update') {
$id = intval($_POST['id']);
$name = sanitize_text_field($_POST['name']);
$description = sanitize_text_field($_POST['description']);
$wpdb->update(
$table_name,
array(
'name' => $name,
'description' => $description,
),
array('id' => $id)
);
答: 暂无答案
评论