2015-12-08 87 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「

相關問題