繁体   English   中英

Ajax返回成功,但不更改数据库

[英]Ajax returns success but doesn't change the database

我正在开发一个小js脚本,以Facebook以前的方式编辑配置文件(单击按钮,进行编辑并保存,而无需重新加载页面)。 问题是,当我运行它时,ajax函数返回成功但对数据库没有任何更改。 os js函数是这样的:

$('.savebtn').click(function(){
            var editdata    = $(".editbox").val();
            var parameter   = $(this).closest("td").find("#parameter").text();

            var datastring  = "data="+editdata+"&parameter="+parameter;

            var $t = $(this);

            console.log(datastring);

            $.ajax({
                type: "POST",
                url: BASE_URL + "/API/update_profile.php",
                data: datastring,
                cache: false,
                success: function()
                {
                    $t.closest('td').find('.curr_value').html(editdata);
                    $t.closest('td').find('.curr_value').hide;
                    console.log(editdata);
                    $(this).prev(".edit").hide();
                    $(this).prev(".curr_value").show();
                    $(this).prev('.edit_link').show();
                    $(this).hide();
                }
            });
        });

(忽略$ t东西,以某种方式这样工作,但是如果我使用$(this)则不是)

Ajax成功执行代码,但不更新数据库上的任何内容。

该数据库的PHP代码为:

<?php

include_once("../../config/connect_db.php");
include_once("../../database/cliente.php");

$parameter = $_POST['parameter'];
$data = $_POST['data'];
$id = $_SESSION['id'];

var_dump($_POST);

try {
    updateProfile($parameter, $data, $id);
}
catch (PDOException $e)
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

?>

function updateProfile($parameter, $data, $id)
{
    global $conn;
    $stmt = $conn->prepare("UPDATE biofood.users 
                            SET ? = ?
                            WHERE id = ?");
    $stmt->execute(array($parameter, $data. $id));
}

编辑:指出,这可能是尝试将列名作为参数传递的问题。 将代码更改为以下内容,但没有成功:

function updateProfile($parameter, $data, $id)
{
    global $conn;
    $query = "UPDATE biofood.users 
              SET $parameter = $data
              WHERE id = $id";
    $stmt = $conn->prepare($query);
    $stmt->execute();
}

这行:

$stmt->execute(array($parameter, $data. $id));

我认为应该是

$stmt->execute(array($parameter, $data, $id));

(注意$data之后的逗号)

这可能无法解决您的问题,但是可以为您提供问题的更好指示。

首先,您没有检查它是否起作用,因为您的updateProfile函数什么也不返回。

修改您的updateProfile函数,使其返回受影响的行数。 (顺便说一句,这是编写函数的一种更安全的方法。如果可以在调用此函数之前检查或限制$ parameter的值,则它不太容易被SQL注入。)

function updateProfile($parameter, $data, $id)
{
    global $conn;
    $stmt = $conn->prepare("UPDATE biofood.users SET $parameter = ? WHERE id = ?");
    $stmt->execute(array($data, $id));
    return $stmt->rowCount(); // # of rows affected
}

在调用此函数的脚本中,获取值并将其作为响应发送回。 我们将发送回JSON。

$response = array();
try {
    $response['success'] = updateProfile($parameter, $data, $id);
} catch (PDOException $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

header('Content-Type: application/json');
echo json_encode($response);

在您的JavaScript文件中,进行以下更改:

$.ajax({
     type: "POST",
     url: BASE_URL + "/API/update_profile.php",
     data: datastring,
     cache: false,
     success: function (data) {
         if (data.success) {
             $t.closest('td').find('.curr_value').html(editdata);
             $t.closest('td').find('.curr_value').hide;
             console.log(editdata);
             $(this).prev(".edit").hide();
             $(this).prev(".curr_value").show();
             $(this).prev('.edit_link').show();
             $(this).hide();
         }
     },
     dataType: 'json'
});   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM