hello everyone
I have the same problem that they talked about here before in this old thread:
http://www.daniweb.com/web-development/php/threads/94707
I tried to find any solution for it but I couldn't
this one "UPDATE table SET col_value = col_value + 1" doesn't work , so I hope that any one can tell me complete solution for this issue to increase Column values by one
thanks

Recommended Answers

All 4 Replies

Make sure your field type is INT.

thanks vibhadevit for reply
I make sure my column is INT, and when I try to update one filed by this way it works fine
but for multiple fields in an array, it overwrite it's values with the (higher value+1)
and this is my code :

$p = array(1, 2, 3);

foreach($p as $z)
{
$sql = "UPDATE table1 SET position='$z'+1 Where position='$z' ";
$result = mysql_query($sql);
if(mysql_affected_rows()==1)
 		{
echo "update Name position Successfuly";
		}else{
			die("database query failed: " . mysql_error());
			 }

}

my database Colums in table1:
id(INT)10 position(INT)10 names(varchar)255
as you can see I want get position= values in this array and increase it by one

This is because it is executing this way,
when first time it try to check pos 1 (from array value) ,increase it and make 2.
Second time it try to check pos 2 (from array value), NOW HERE from above first iteration you have also made pos 2. So here update will affect 2 row.
so on..
Solution is firstly you need to get all unique ID field for which you want to increase pos.
Then in update query use that unique id.
i.e. "UPDATE table1 SET position='$z'+1 Where id='$stored_id' ";

commented: Thanks for great support +0

Your solution works fine, thank you vibhadevit for help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.