I am trying to add a number to a number already in a MySQL table. I am using POST to pass in the number.
<?php
require "connection.php";
$id = $_POST["id"];
$number = $_POST["number"];
Do I have to make 2 separate queries, one to retrieve the current value and set is as a variable and one to insert the new value by adding the number to the variable or can I accomplish this with one query with something like this:
$update_query = "update Table set value = (value + $number) where id like '$id';"
update field to add value to existing value:
UPDATE table SET value = value + $number WHERE id = $id;
update field to contact value to existing value:
use the CONCAT function :
UPDATE table SET value= CONCAT('$number',value) WHERE id=$id;
Yes, you can use one query UPDATE tbl_name SET value_col = value_col + :number WHERE id = :id
Notes
ID is unique, so you need id = :id
to achieve faster queries and only update one field.
Use prepared statements instead of putting values directly to query