3

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)

2
  • What are you incrementing? Since you are adding a new row (which by default will have a count of 0) it seems logical that the result will be 10 each time. If you do an update, it should be better. Commented Oct 4, 2011 at 8:59
  • sorry i forgotten to add in the update script , just edited Commented Oct 4, 2011 at 9:01

6 Answers 6

9

try this syntax

update table1 set count = count + $countv where id = '123'
Sign up to request clarification or add additional context in comments.

Comments

0

you can do this in two ways: - first retrieve the value, update it with php and write it back to the mysql table - or a simple sql query: UPDATE leaderb SET count = count + 10 WHERE ....

since you are inserting in mysql you don't have an initial value there. so you should use update to change an existing row/record.

1 Comment

Normally i add 10 using +10 like you said , but now i want to try adding it via a variable. so i set "count" column to add $countv's value interger.
0

Your update query should be:

$query ="UPDATE table1 `count` = `count` + $countv WHERE id=123";

Mind you, count is a reserved word in MySQL so it should be in backticks

Comments

0

i have no idea how it worked for you since u add the names '$id' and '$countv' instead of thier values.

i think you should swap that:

$insert  = "INSERT INTO table1 (id,count) VALUES ('$id',+$countv)";

with:

$insert  = "INSERT INTO table1 (id,count) VALUES ('$id', +" . $countv . ")";

if id is also a php int (i assume so) u need:

$insert  = "INSERT INTO table1 (id,count) VALUES ('" . $id . "', +" . $countv . ")";

tell me how it went :)

Comments

0

Its because you are replacing the value each time with 10. The program does not know the number that is in the database.

You need to retrieve the data first, update the value and then place it back in the database

Comments

0

Try this:

UPDATE table1 SET count = count+$countv WHERE id = '123'

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.