0

I have a value in my MYSQL database, all I want to do is to increase the current value with a new one, this is what I have tried

    } elseif ($gametype == "veckanskluring"){
    $sql = "UPDATE users SET veckanskluring='veckanskluring'+'$score' WHERE id='$id'";
$retval = mysql_query( $sql, $link );
        echo "GAME == $gametype";
}

But for some odd reason, this won't work. I have searched online and found examples, but they all look, almost exactly the same as my code.

// Sidenote this is not the whole code, obviously. Everything except the part where I add the new value to the old value works, and if I remove 'veckanskluring'+ it updates without any problems. I strongly believe something is wrong with this part - 'veckanskluring'+ as the other part works fine. //NOTE2 score is always 999, just have it set to $score if I want to change it later.

UPDATE -

MY fault, apparently I had put '' around veckanskluring.

$sql = "UPDATE users SET veckanskluring=veckanskluring +'$score' WHERE id='$id'"; <-- Working.
Jonas
  • 85
  • 11
  • You only need quotes in your SQL statements round literal values, not column names (they become strings) or numeric values. – Nigel Ren Mar 11 '18 at 17:01

1 Answers1

0

Assuming that $score and $id are number you shoudl not use sigle quote around this vars

and assuming that veckanskluring is column name you must not use single quote aroud column name

"UPDATE users SET veckanskluring= veckanskluring +$score WHERE id=$id";

But the use of php var in sql is deprecated you at risk for sql injection .. take a look at your mysql driver for bindig param

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107