Emin Eryilmaz
Emin Eryilmaz

Reputation: 35

PHP won't update +1 value on mysql

$sql="UPDATE users SET TN4='1', Notificare4='1', Neachitate='1' WHERE username='$user'";

This works fine. It sets Neachitate row into 1. But, I want to set it to $Neachitate+ 1 ( +1 value, like each time you click it adds one more)

So, I modified it with

$Neachitate = $row['Neachitate']; // it gets what value is in Neachitate, right?

    $sql3="UPDATE users SET TN4='1', Notificare4='1', Neachitate='$Neachitate + 1' WHERE username='$user'";

But this time it sets to 0. Why?

Upvotes: 1

Views: 64

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269543

More generally, eliminate the single quotes! And use parameters!

UPDATE users
    SET TN4 = 1,
        Notificare4 = 1,
        Neachitate = ? + 1
    WHERE username = ?;

Single quotes should only be used for string and date constants. Parameters should always be used for passing constant values into queries.

Upvotes: 1

Federkun
Federkun

Reputation: 36924

Use

$sql3="UPDATE users SET TN4='1', Notificare4='1', Neachitate=Neachitate + 1 WHERE username='$user'";

instead.

You're trying to save Neachitate='$Neachitate + 1': this is a string, not a number.

Upvotes: 1

Related Questions