开发者

mysql update statement

开发者 https://www.devze.com 2023-03-28 08:44 出处:网络
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 wo

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开发者_StackOverflow社区 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);


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消