Help updating existing mySQL entry instead of creating new row everytime.

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.

a)get away from mysql functions. use mysqli or my personal choice pdo.

either way, what you want is on duplicate key update. for that, your database table needs a unique key. htne you would modify your query to this:

[php] $order=“INSERT INTO player_data (id,game_id,gold) VALUES (’$id_row’,’$_GET[uid]’,’$_GET[gold]’) on duplicate key update game_id=values(’$_GET[uid]’), gold=values(’$_GET[gold]’)”;

Thank you, I’m locking this though because of duplicate post from when I was just on the website without account first. But my google searches come up with the same results.

And on that advise have your website hacked in seconds.

Take a look at the big fat deprecated notice here:

his extension is deprecated as of PHP 5.5.0, and will be removed in the future.

This means that one day, when your host upgrades the PHP version… you’ll wake up to a nice broken website as all of your mysql_queries will fall flat on their face with a big fat fail.

I’m not rewriting the query to PDO or MySQLi – Do this yourself and if you have trouble, write back and we’ll help you out.

Part of programming and development is keeping up to date with modern standards. Keep yourself updated all the time as technology moves on and leaves people behind way too easily!

Sponsor our Newsletter | Privacy Policy | Terms of Service