0

I am trying to update "new" column value with new value but problem is my query remove previous data while inserting new value

What is want: here is example table structure,

Table name = agg_lvl primary key set = uid

uid     | new
--------|--------
1       | 100
2       | 300

You can see "new" has 100 points, for example I send 100 new points to user 1, so new column value should be 100 + 100 = 200, right now with this code

$query4 = mysql_query("INSERT INTO agg_lvl (uid, new) VALUES ('$uid','$new')
ON DUPLICATE KEY UPDATE uid='$uid',new='$new'");

Not sure what

new = '$new'

I have tried both ways but no success = >

new = 'new + $new' or new = new + '$new'
4
  • 2nd: new=new+$new exectly = ON DUPLICATE KEY UPDATE new=new+$new Commented May 1, 2016 at 8:27
  • near miss :( so the problem was because of '' i remove it as per your instructions and now able to get my desire results Commented May 1, 2016 at 9:00
  • please post an answer, in comments i have no control to accept your answer, i am asking this because your answer is perfect and credit should gone to where it due, so it's due on you Commented May 1, 2016 at 9:12
  • i am back to computer and writing the answer Commented May 1, 2016 at 9:55

3 Answers 3

2

You should make changes in your query

  1. Make num = nun+$num to add new value to old one
  2. Remove quotes arount $new because it is a number but not a string
  3. Remove uid from set list because insert already point to that record

And your query should look so:

$query4 = mysql_query("INSERT INTO agg_lvl (uid, new) VALUES ('$uid','$new')
ON DUPLICATE KEY UPDATE new=new+$new");
Sign up to request clarification or add additional context in comments.

15 Comments

one new and strange thing happened
could you a little bit more?
it's working Perfect, one new and strange thing happened, the problem is since "id" i set Auto Increment and primary key, so when this query fire, it inserting new row every-time, the reason is " ON DUPLICATE KEY UPDATE " not working, because of " KEY " case, do you have any idea about how to deal with should i need to assign unique name to column " date " so we able to update row if date is same, if yes than how ? so we have total 4 columns in db , ( id, uid, new, date )
what is uid in this case?
uid is coming from form posting
|
0

Okay first i will answer with the proper way to do the same, In this case i am assuming that UID is unique, so you make a new table scorecard with UID as foreign key. Now rather than update, you just insert stuff to table like if UID 1 gains 10 and 20 points, there are two entries. onw with 10 and one with 20. Now to get his current points, you add all points where UID=1 .

Now in your implementation the correct query would be

UPDATE userData SET points = points + x WHERE UID = $uid

where x is the new points gained and points is the name of column

5 Comments

@Rahul thanks for the catch, happened because i added the where statement in an edit and forgot to check the syntax again :)
2nd: new=new+$new exectly = ON DUPLICATE KEY UPDATE new=new+$new – splash58, this answer is working for me now
@Goarge Go and why have you accepted not the answer that give you the result ? :) Accept your one
because the guy who answer send comments he didn't posted answer, i guess he is new here
when i try to accept my answer it says " accept your own answer in next 2 days ?
0

$query4 = mysql_query("INSERT INTO agg_lvl (uid, new) VALUES ('$uid','$new') ON DUPLICATE KEY UPDATE uid='$uid',new=new+$new");

worked for me with help of @splash58

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.