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);
INSERT ON DUPLICATE KEY UPDATEshould do what you’re looking for ifusernameis a primary or unique key. More information in the manual.