1

I have records on my table and I have to update the records.if records are available then add the same value which is already in a database.

For example: I have column name aMan and value is 30 so I have to update same value and new values will be 60.

//Total column value=column value + column value 

if ($check_record >0) { 
    // will check the records available or not
    $sql="UPDATE man SET aMan = '$action_points' where user_id='$id'";
} else {
    $new=$column_value + $column_value;
    $sql="UPDATE man SET aMan = '$new' where user_id='$id'";
}

In short I have to add the value.Would you help me in this?

enter image description here

2 Answers 2

1

The following will double the aMan value:

$sql = "UPDATE man SET aMan=aMan*2 WHERE user_id='$id'";

If you only need to add a specific value and not the same value as the column already has, use:

$sql = "UPDATE man SET aMan=aMan+30 WHERE user_id='$id'";

If you need to update multiple columns at the same time, use something like this:

$sql = "UPDATE man SET aMan=aMan+30, bMan=bMan+20 WHERE user_id='$id'";

Also, I suggest you to use prepared statements with mysqli or PDO instead of adding an $id variable diretly into your query string, which is very unsafe.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for replying Mr.Yury, In case i have to update every time 30 than what qyery i have to use it?
Where should I use that query inside if or else?
Inside your else statement, instead of both lines
Mr.Yury, I have one more doubt.In case there are more than on column then what will be update query i have to use it?
I don't know Mr.Yury, I am getting lots of doubt now.
|
0

This can be a another approach.

$sql="UPDATE man SET aMan = aMan + aMan where user_id='$id'";

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.