Naren Verma
Naren Verma

Reputation: 2307

How to add same value with help of update query using PHP?

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

Upvotes: 1

Views: 1083

Answers (2)

Yury Fedorov
Yury Fedorov

Reputation: 14928

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.

Upvotes: 1

Manoj Sharma
Manoj Sharma

Reputation: 1465

This can be a another approach.

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

Upvotes: 0

Related Questions