Ajax PUT请求成功,但未在数据库上更新

时间:2019-05-03 00:33:23

标签: javascript ajax

我正在执行一个AJAX PUT请求,该请求返回成功但未在数据库上更新。

在转换为JSON之前已进行检查,以确保进入数组的变量正确无误。

var recycle = {
    id:document.getElementById('id').value,
    year:document.getElementById('year').value,
    materialThousandTonnes:document.getElementById('material').value,
    packagingWasteArising:document.getElementById('packaging').value,
    totalRecoveredRecycled:document.getElementById('total').value,
    achievedRecoveryRecyclingRate:document.getElementById('achieved').value,
    euTargetRecoveryRecyclingRate:document.getElementById('target').value
};

$.ajax({
    url: '/recycle',
    method: 'PUT',
    data: JSON.stringify(recycle),
    contentType: "application/json;charset=utf-8",
    success: function (data) {
        alert("hi");
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert(jqXHR + '\n' + textStatus + '\n' + errorThrown);
    }
});

警报“ hi”每次都会出现,但实际上并没有将数据放入我的数据库中,请帮忙!

编辑:

这是用于PUT请求的服务器端代码

 public function performPut($url, $parameters, $requestBody, $accept) 
    {
        global $dbserver, $dbusername, $dbpassword, $dbdatabase;

        $newRecycle = $this->extractRecycleFromJSON($requestBody);
        $connection = new mysqli($dbserver, $dbusername, $dbpassword, $dbdatabase);
        if (!$connection->connect_error)
        {
            $sql = "update recycledb set year = ?, materialThousandTonnes = ?, packagingWasteArising = ?, totalRecoveredRecycled = ?, achievedRecoveryRecyclingRate = ?, euTargetRecoveryRecyclingRate = ? where id = ?";
            // We pull the fields of the book into local variables since 
            // the parameters to bind_param are passed by reference.
            $statement = $connection->prepare($sql);
            $id = $newRecycle->getId();
            $year = $newRecycle->getYear();
            $materialThousandTonnes = $newRecycle->getMaterialThousandTonnes();
            $packagingWasteArising = $newRecycle->getPackagingWasteArising();
            $totalRecoveredRecycled = $newRecycle->getTotalRecoveredRecycled();
            $achievedRecoveryRecyclingRate = $newRecycle->getAchievedRecoveryRecyclingRate();
            $euTargetRecoveryRecyclingRate = $newRecycle->getEuTargetRecoveryRecyclingRate();
            $statement->bind_param('iisiiss', $id, $year, $materialThousandTonnes, $packagingWasteArising, $totalRecoveredRecycled, $achievedRecoveryRecyclingRate, $euTargetRecoveryRecyclingRate);
            $result = $statement->execute();
            if ($result == FALSE)
            {
                $errorMessage = $statement->error;
            }
            $statement->close();
            $connection->close();
            if ($result == TRUE)
            {
                // We need to return the status as 204 (no content) rather than 200 (OK) since
                // we are not returning any data
                $this->noContentResponse();
            }
            else
            {
                $this->errorResponse($errorMessage);
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

所以我经过艰苦的努力找到了解决问题的方法

在这一行:

$statement->bind_param('iisiiss', $id, $year, $materialThousandTonnes, $packagingWasteArising, $totalRecoveredRecycled, $achievedRecoveryRecyclingRate, $euTargetRecoveryRecyclingRate);

id变量需要移到语句的末尾...

赞:

$statement->bind_param('iisiiss', $year, $materialThousandTonnes, $packagingWasteArising, $totalRecoveredRecycled, $achievedRecoveryRecyclingRate, $euTargetRecoveryRecyclingRate, $id);

答案 1 :(得分:-1)

$statement->bind_param('iisiiss', $year, $materialThousandTonnes, $packagingWasteArising, $totalRecoveredRecycled, $achievedRecoveryRecyclingRate, $euTargetRecoveryRecyclingRate, $id);