473,378 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Adding to an existing value. (integer)

11
[PHP]<?php

//this function is the responsible of modifying the player in the table
function modify_table()
{
//first the query try to get a row where the name of the player is = to the name in the url
$query = "SELECT name FROM hi WHERE name='".$_GET["name"]."'";
$res = mysql_query($query) or die("Couldn't execute $query: ".mysql_error());

//if the query returns 0 rows the player doesn't exist, if it returns 1 the player already exists
if (mysql_num_rows($res)==0)
{
//the query makes a new row with the player name and the score
$query = "INSERT INTO hi VALUES('".$_GET["name"]."',".$_GET["score"].")";
mysql_query($query) or die("Couldn't execute $query: ".mysql_error());
}
else
{
//the query looks for the row with name=name in the url and updates his score
$query = "UPDATE hi SET score=".$_GET["score"]." WHERE name='".$_GET["name"]."'";
mysql_query($query) or die("Couldn't execute $query: ".mysql_error());
}
}

?>[/PHP]

This is a snippet of my code. When i send in a request to update a player's score, it overwrites the score already there with the new value. What i want it to do is ADD to the old score (eg. old score= 100 submitted score= 50 new score =150

How would i go about this?
Mar 16 '08 #1
9 2356
Markus
6,050 Expert 4TB
Have a look at this:
[php]
$_result = # mysql_query("SELECT ... ");
$_new_score = # get the new score to be added
while($_row = mysql_fetch_array($_result))
{
$_old_score = $_row['score']; # gets the old score
$_updated_score = $_old_score + $_new_score; # add them together
mysql_query("UPDATE ... ") or die("..."); // update here with updated_score.
}
[/php]
Any questions?

Regards
Mar 16 '08 #2
ronverdonk
4,258 Expert 4TB
Your first query must also select the score[php]$query = "SELECT name, score FROM hi WHERE name='".$_GET["name"]."'";
$row=mysql_fetch_assoc($res)
or die ("blahblah..");
[/php]
You just add the new score to it and insert[php]
$val=$row['score']+$_GET['score'];
$query = "UPDATE hi SET score=$val WHERE name='".$_GET["name"]."'";[/php]P.S> Your page, when you run it as you show here, is hacker's heaven!

Ronald
Mar 16 '08 #3
Snaked
11
Thank you for the answer, my code has many encryptions elsewhere, that was just a snippet. edit- is this a direct code? can i copy/paste? or do i need to adapt it? i don't mind either way...
Mar 16 '08 #4
Markus
6,050 Expert 4TB
Thank you for the answer, my code has many encryptions elsewhere, that was just a snippet. edit- is this a direct code? can i copy/paste? or do i need to adapt it? i don't mind either way...
Depends who you're asking?
Mar 16 '08 #5
Snaked
11
i would be talking to roverdonk thank you... i tried using it directly, but it didn't work
Mar 16 '08 #6
ronverdonk
4,258 Expert 4TB
i would be talking to roverdonk thank you... i tried using it directly, but it didn't work
Then nuse this[php]<?php
//this function is the responsible of modifying the player in the table
function modify_table()
{
//first the query try to get a row where the name of the player is = to the name in the url
$query = "SELECT name, score FROM hi WHERE name='".$_GET["name"]."'";
$res = mysql_query($query)
or die("Couldn't execute $query: ".mysql_error());

//if the query returns 0 rows the player doesn't exist, if it returns 1 the player already exists
if (mysql_num_rows($res)==0)
{
//the query makes a new row with the player name and the score
$query = "INSERT INTO hi VALUES('".$_GET["name"]."',".$_GET["score"].")";
mysql_query($query) or die("Couldn't execute $query: ".mysql_error());
}
else
{
$row=mysql_fetch_assoc($res) ;
$val=$row['score']+$_GET['score'];
//the query looks for the row with name=name in the url and updates his score
$query = "UPDATE hi SET score=$val WHERE name='".$_GET["name"]."'";
mysql_query($query) or die("Couldn't execute $query: ".mysql_error());
}
}
?>[/php]Ronald
Mar 16 '08 #7
Snaked
11
Thank you roverdonk! This question is solved.
Mar 16 '08 #8
Snaked
11
Oh, and by the way, the php action for modify_table is sent silently by an exe made by me
Mar 16 '08 #9
ronverdonk
4,258 Expert 4TB
Thank you roverdonk! This question is solved.
Ok, then see you next time here again.

Ronald
Mar 16 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Claudio Lapidus | last post by:
Hello Now perhaps this is a bit dumb, but... I just populated a new table via \copy. After that, I realize that perhaps is a good thing to have a row identifier in it, so I try clapidus=>...
0
by: Sileesh | last post by:
Hi I have html table and a Button in an Aspx page. I am adding one row with some textboxes to Html table each time i click on the Button thru Javascript. Now problem is when when i try to...
0
by: Adam J. Schaff | last post by:
Hello. I have a custom collection that implements IBindingList (allownew and allowremove are both true). I have bound it to a datagrid. I have add and remove buttons on the screen. I want to...
0
by: Andre Viens | last post by:
Hello, I am using the following variation of code from <http://support.microsoft.com/default.aspx?scid=kb;EN-US;319340> to add icons to an imagelist for use in a listview: Private Structure...
6
by: Rudy | last post by:
Hi all, I know this is easy, just can't seem to get it. I have a windows form, and a text box, with a value already in it. I need to add that value to a table. It's just one value, so the entire...
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
1
by: Dixie | last post by:
I want to append a number of records onto an existing table. Each record will have an ID number (it is not an autonumber as it is too variable). It is currently set as an integer field. I want...
5
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want...
7
by: Miro | last post by:
Im a VB Newbie so I hope I'm going about this in the right direction. I have a simple DB that has 1 Table called DBVersion and in that table the column is CurVersion ( String ) Im trying to...
1
by: Allen | last post by:
I need a way to add a method to an existing instance, but be as close as possible to normal instance methods. Using 'new' module or such code as 'def addfunc(...): def helper(...) .. setattr(...)'...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.