Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding integers from a variable to mysql column

Tags:

php

mysql

i'm wondering how can i add an integer which i have assigned in a variable to a mysql table's column using INSERT.

this is my code:

//Connect to database
include ("connnect.php");

$countv = "10"
$insert  = ("INSERT INTO table1 (id,count) VALUES ('$id',+$countv)";
mysql_query($insert) or die(mysql_error());

the count column is set to int and the default value is '0' Normally i add 10 using +10 , but now i want to try adding it via a variable. so i set "count" column to add $countv's value interger.

this script can add 10 to the column count, but when i try it the second time(which is using UPDATE table1 SET count = +$countv WHERE id='123') , it still remains as 10. is there any mistakes i'm making or is there a better way to do this?

Update script

  mysql_query("UPDATE table1 SET count = + $countv WHERE id='123'")
            or die(mysql_error()); 

Thanks and have a nice day.

(SOLVED : i forgotten to add count infront of + $countv in the update script. thanks for those who help)

like image 964
ETAN Avatar asked May 20 '26 22:05

ETAN


1 Answers

try this syntax

update table1 set count = count + $countv where id = '123'
like image 106
bumperbox Avatar answered May 22 '26 13:05

bumperbox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!