简体   繁体   English

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

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

I'm developing a small script of js to edit a profile in the way facebook used to be (click a button, edit and save without reloading the page). 我正在开发一个小js脚本,以Facebook以前的方式编辑配置文件(单击按钮,进行编辑并保存,而无需重新加载页面)。 The problem is that when I run it, the ajax function returns sucess but akes no changes on the database. 问题是,当我运行它时,ajax函数返回成功但对数据库没有任何更改。 The function os js is this: 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();
                }
            });
        });

(Ignore the $t thing, somehow this works like this, but not if I use $(this)) (忽略$ t东西,以某种方式这样工作,但是如果我使用$(this)则不是)

Ajax executes the code for sucess but doesn't update anything on the database. Ajax成功执行代码,但不更新数据库上的任何内容。

The PHP code for the database is: 该数据库的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));
}

EDIT: As pointed out, this could be a problem with trying to pass a column name as a parameter. 编辑:指出,这可能是尝试将列名作为参数传递的问题。 Changed the code to the following, but with no sucess: 将代码更改为以下内容,但没有成功:

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

This line: 这行:

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

I think should be 我认为应该是

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

(notice the comma after $data ) (注意$data之后的逗号)

This might not solve your problem, but it might give you a better indication on where your problem is. 这可能无法解决您的问题,但是可以为您提供问题的更好指示。

First, you are not checking whether it works or not as your updateProfile function returns nothing. 首先,您没有检查它是否起作用,因为您的updateProfile函数什么也不返回。

Modify your updateProfile function, so that it returns the number of rows affected. 修改您的updateProfile函数,使其返回受影响的行数。 (BTW this is a safer way to write your function. If you can check or limit the value of $parameter prior to calling this function, it will be less prone to SQL injection.) (顺便说一句,这是编写函数的一种更安全的方法。如果可以在调用此函数之前检查或限制$ 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
}

In the script that calls this function, get the value and send it back as a response. 在调用此函数的脚本中,获取值并将其作为响应发送回。 We'll send back a JSON. 我们将发送回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);

In your JavaScript file, make the following change: 在您的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