使用jquery和php在数据库中手动增加字段

时间:2015-12-08 19:48:23

标签: javascript php jquery mysql ajax

我正在尝试构建一个单击按钮,该按钮会增加数据库中项目的值。我正在使用UPDATE方法。

问题在于,每当运行更新查询时,从数据库到递增(或递减)的值都为零。 (0 + 1 = 1,0-1 = -1)

require_once("C:/xampp/htdocs/Selfie/database/dbcontroller.php");
$db_handle = new DBController();

$image_id = $_POST["image_id"];
$active_user_id = $_POST["active_user_id"];
$query = "SELECT user_image_id from users where user_id='" . $active_user_id . "'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if ($row['user_image_id'] == $image_id) {
    echo "own image";
}
else
{
    $query = "SELECT image_id from hearts where user_id='" . $active_user_id . "'";
    $result = mysql_query($query);
    if ($row = mysql_fetch_assoc($result)) {
        if ($row['image_id'] == $image_id) {

            $query = "UPDATE images SET image_hearts='image_hearts'-1 where image_id=" . $image_id;
            $result = mysql_query($query);

            $query = "DELETE FROM hearts WHERE user_id=" . $active_user_id;
            $result = mysql_query($query);


            $query = "UPDATE users SET user_like ='' where user_id=" . $active_user_id;
            $result = mysql_query($query);
            echo "just unlike";
        }
        else
        {

            $query = "DELETE FROM hearts WHERE user_id=" . $active_user_id;
            $result = mysql_query($query);

            $query = "UPDATE images SET image_hearts='image_hearts'-1 where image_id=" . $row['user_image_id'];
            $result = mysql_query($query);

            $query = "Select image_path from images where image_id=" . $image_id;
            $result = mysql_query($query);
            $row = mysql_fetch_assoc($result);

            $query = "UPDATE users SET user_like ='" . $row["image_path"] . " where user_id=" . $active_user_id;
            $result = mysql_query($query);

            $query = "UPDATE images SET image_hearts='image_hearts'+1 where image_id=" . $image_id;
            $result = mysql_query($query);

            $query = "INSERT INTO hearts (image_id , user_id) VALUES ('$image_id','$active_user_id')";
            $result = mysql_query($query);
            echo "unlike then like";
        }
    }
    else
    {

        $query = "INSERT INTO hearts (image_id , user_id) VALUES ('$image_id','$active_user_id')";
        $result = mysql_query($query);

        $query = "UPDATE images SET image_hearts='image_hearts'+1 where image_id=" . $image_id;
        $result = mysql_query($query);

        $query = "Select image_path from images where image_id=" . $image_id;
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);

        $query = "UPDATE users SET user_like ='" . $row["image_path"] . "' where user_id=" . $active_user_id;
        $result = mysql_query($query);

        echo "image liked successfully.";
    }
}

这是我的jQuery代码:

function test_click(i_image_id, i_heart_id, i_active_user_id) {
    var active_user_id = i_active_user_id;
    var image_id = i_image_id;
    var heart_id = i_heart_id;
    jQuery.ajax({
        url: "../Selfie/validations/add_like.php",
        data: {
            active_user_id: active_user_id,
            image_id: image_id
        },
        type: "POST",
        success: function(data) {
            if (data == "own image")
            {
                alert('You are trying to like your own image You NARCISSIST');
            }
            else if (data == "just unlike")
            {
                $("*").removeClass("btn-heart-red animated bounce fa-heart-red");
                alert('just unlike');
            }
            else
            {
                $("*").removeClass("btn-heart-red animated bounce fa-heart-red");

                $("#" + heart_id).removeClass("animated rubberBand");
                $("#" + heart_id).toggleClass("btn-heart-red animated bounce fa-heart-red");

            }
            alert(data);
        }
    });
}

1 个答案:

答案 0 :(得分:2)

image_hearts='image_hearts'+1删除引号;这是你想要更新的列,而不是字符串文字。 'image_hearts'-1

也是如此

检查您的查询错误,这对您有所帮助。

另外,您目前的代码向SQL injection开放。使用mysqli with prepared statementsPDO with prepared statements

mysql_*函数弃用通知:

http://www.php.net/manual/en/intro.mysql.php

从PHP 5.5.0开始,不推荐使用此扩展,从PHP 7.0开始删除此扩展,不建议用于编写新代码,因为将来会删除它。相反,应使用mysqliPDO_MySQL扩展名。在选择MySQL API时,另请参阅MySQL API Overview以获得进一步的帮助。

这些功能允许您访问MySQL数据库服务器。有关MySQL的更多信息,请访问»http://www.mysql.com/

可以在»http://dev.mysql.com/doc/找到MySQL的文档。

脚注:

如果我引用Marc的评论:

  换句话说,

“。image_hearts'+ 1是字符串文字加整数,除非该字符串文字在其开头包含数字,否则将简单地变为0 + 1 - Marc B” < / p>