0

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

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Green-z
0

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

Edited by Green-z: n/a

1

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' ";

Votes + Comments
Thanks for great support
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.