Update existing mySQL row if it exists instead of creating new row.

Hello some friends and I are working on a modification for a video game. The game sends data to PHP to be stored and taken from the mySQL database.

The script in the mod attached to this PHP code sends $uid “the player’s game id used to identify the player” and $gold “the amount of gold they have on them” to this script. It is triggered when a player leaves the game.

The problem is when the script is triggered, it creates a new mySQL entry everytime, and we need it to only create a new entry if there is no matching $uid “user id”. If there is a matching $uid we want it to update the player’s $gold. So $uid is what is needed to identify accounts on the game and in the database. Here is the code.

if (!$connect)
die('Could not connect: ’ . mysql_error());
$rows = mysql_query(“SELECT * FROM player_data”, $connect);
$num_row = mysql_num_rows($rows);
$id_row = $num_row + 1;

$order=“INSERT INTO player_data (id,game_id,gold) VALUES (’$id_row’,’$_GET[uid]’,’$_GET[gold]’)”;

if (!mysql_query($order,$connect))
die('Error: ’ . mysql_error());

How would we go about updating values in existing accounts, not only for the script but the rest connected to the project. Thank you!

p.s. I am not the php coder of the project, the coder told me what his problem was, and he seems to not know what command to use to update. I am the project overseer pretty much, but semi understand both the modding system and the php codes we are using.

You need an update query.

Have a look at http://www.w3schools.com/php/php_mysql_update.asp

Sponsor our Newsletter | Privacy Policy | Terms of Service