user172697
user172697

Reputation: 155

mysql update statement

I have this statement which calls insert data to table "users" when table "users" already has data. I need to update the count of users every time i load this script. My update statement isn't working.

Any clue? No update happened and the old numbers still exist.

$query = sprintf("INSERT INTO users VALUES ('%s','%s',%s)",
                mysql_real_escape_string($user['username']),
                mysql_real_escape_string($user['name']),
                mysql_real_escape_string($user['countusers']),
            $res = mysql_query($query);
            $query = "UPDATE users (username, name, countuser) VALUES ('$username',        '$name', '$countuser')";
            $res = mysql_query($query);

Upvotes: 1

Views: 198

Answers (2)

Michael Mior
Michael Mior

Reputation: 28752

INSERT ON DUPLICATE KEY UPDATE should do what you're looking for if username is a primary or unique key. More information in the manual.

Upvotes: 1

PeeHaa
PeeHaa

Reputation: 72662

I don't see where you check whether the user already exists.

Also in the insert query you use:

$user['username'], $user['name'] and $user['countuser'];

but in the update query you do:

$username, $name and $countuser

Upvotes: 0

Related Questions