2015-12-08 78 views
0

我正在尝试构建一个单击按钮,以增加数据库中项目的值。我正在使用UPDATE方法。使用jquery和php在数据库中手动增加字段

问题是,无论何时运行更新查询,从数据库到增量(或减量)所需的值都是零。 (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); 
     } 
    }); 
} 
+2

请[停止使用'mysql_ *'函数](http:// stackov erflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [这些扩展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中删除。了解[编写]​​(http://en.wikipedia.org/ wiki/Prepared_statement)语句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)并考虑使用PDO,[这真的很简单](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+1

[您的脚本存在SQL注入攻击风险。](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –

+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 Overview以获取进一步帮助,同时选择MySQL API。

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

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


脚注:

如果我可以引述马克的评论:

“换句话说‘image_hearts’+ 1是字符串字面量正整数,除非该字符串常量包含数字在它的开始,将简单地变成0 + 1 - Marc B“

相关问题