karimali831 Posted May 31, 2012 Share Posted May 31, 2012 I am trying to update 7 rows by setting the "wk" column in the sequence 1-7, so each row will have "wk" set to 1-7, right now the query is updating all rows and setting wk column to 7 and not 1-7? for($i = 1; $i <= 7; $i++) { safe_query("UPDATE ".PREFIX."cup_matches SET wk='$i' WHERE type='gs' && $type_opp='0' && matchno='$ID' && clan1='".$sPT['clan1']."'"); } Any help on this please? Quote Link to comment https://forums.phpfreaks.com/topic/263406-for-loop/ Share on other sites More sharing options...
smerny Posted May 31, 2012 Share Posted May 31, 2012 nothing is changing in the WHERE you are setting whatever rows match your WHERE to 1, then to 2, then to 3, etc... Quote Link to comment https://forums.phpfreaks.com/topic/263406-for-loop/#findComment-1349972 Share on other sites More sharing options...
karimali831 Posted May 31, 2012 Author Share Posted May 31, 2012 The WHERE gives you 7 rows and all these rows have the wk value set to "7" ? Or am I still not understanding? Sorry Quote Link to comment https://forums.phpfreaks.com/topic/263406-for-loop/#findComment-1349973 Share on other sites More sharing options...
Psycho Posted May 31, 2012 Share Posted May 31, 2012 I am trying to update 7 rows by setting the "wk" column in the sequence 1-7, so each row will have "wk" set to 1-7, right now the query is updating all rows and setting wk column to 7 and not 1-7? Can you try explaining that again? What do you mean by setting the value to "1-7". The code you have sets the value to 1, then on the 2nd iteration sets it to 2, then 3, then 4, etc. So, when the loop completes the record will only have the value of 7. If you want a value of "1-7" then set that value. Instead of running the queries you should echo them to the page to see what is actually happening. That is why I advise to always create queries as a string variable instead of building them directly i the mysql_query() function. for($i = 1; $i <= 7; $i++) { $query = "UPDATE ".PREFIX."cup_matches SET wk='$i' WHERE type='gs' && $type_opp='0' && matchno='$ID' && clan1='{$sPT['clan1']}'"; echo $query . "<br>\n"; //safe_query($query); } Quote Link to comment https://forums.phpfreaks.com/topic/263406-for-loop/#findComment-1349974 Share on other sites More sharing options...
karimali831 Posted May 31, 2012 Author Share Posted May 31, 2012 This is what I get and I want it like this:- UPDATE ws_bi2_cup_matches SET wk='1' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1' UPDATE ws_bi2_cup_matches SET wk='2' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1' UPDATE ws_bi2_cup_matches SET wk='3' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1' UPDATE ws_bi2_cup_matches SET wk='4' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1' UPDATE ws_bi2_cup_matches SET wk='5' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1' UPDATE ws_bi2_cup_matches SET wk='6' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1' UPDATE ws_bi2_cup_matches SET wk='7' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1' But when I select: SELECT * FROM `ws_bi2_cup_matches` WHERE WHERE type='gs' && ladID='0' && matchno='6' && clan1='1' Each 7 rows has a set value of "7" ? Pic: http://s16.postimage.org/i3zumstv7/prob6.png 1st row should have 1.. 2nd row 2.. 3rd row 3.. so forth. Quote Link to comment https://forums.phpfreaks.com/topic/263406-for-loop/#findComment-1349976 Share on other sites More sharing options...
Porl123 Posted May 31, 2012 Share Posted May 31, 2012 On an unrelated note you should probably try using the AND keyword in your WHERE clauses rather than double ampersand. Quote Link to comment https://forums.phpfreaks.com/topic/263406-for-loop/#findComment-1349977 Share on other sites More sharing options...
Pikachu2000 Posted May 31, 2012 Share Posted May 31, 2012 How are you expecting to update seven different records with the same WHERE clause? Do you see why that doesn't make any sense, and will never work? Quote Link to comment https://forums.phpfreaks.com/topic/263406-for-loop/#findComment-1349978 Share on other sites More sharing options...
karimali831 Posted May 31, 2012 Author Share Posted May 31, 2012 How are you expecting to update seven different records with the same WHERE clause? Do you see why that doesn't make any sense, and will never work? I do understand what you are saying but you still unsure what I am trying to do? I am able to fetch 7 rows and simply want to update each row to set value 1,2,3,4,5,6 and 7. Quote Link to comment https://forums.phpfreaks.com/topic/263406-for-loop/#findComment-1349980 Share on other sites More sharing options...
Pikachu2000 Posted May 31, 2012 Share Posted May 31, 2012 Then you need to identify each row by a unique identifier, most likely its primary key. Quote Link to comment https://forums.phpfreaks.com/topic/263406-for-loop/#findComment-1349981 Share on other sites More sharing options...
jcanker Posted June 29, 2012 Share Posted June 29, 2012 Did you solve this? This query will always result in wk == 7 because your WHERE doesn't differentiate between themselves, so the same rows are getting updated, first with 1, then with 2, then 3, etc, until 7. The problem isn't necessarily your code as much as the table structure or the way you're querying it. Seems to me that maybe this shouldn't be an UPDATE so much as an INSERT query. Quote Link to comment https://forums.phpfreaks.com/topic/263406-for-loop/#findComment-1357859 Share on other sites More sharing options...
Barand Posted June 29, 2012 Share Posted June 29, 2012 Perhaps if you now add another condition "AND wk > $i" for($i = 1; $i <= 7; $i++) { $query = "UPDATE ".PREFIX."cup_matches SET wk='$i' WHERE type='gs' AND $type_opp='0' AND matchno='$ID' AND clan1='{$sPT['clan1']}' AND wk > $i LIMIT 1"; echo $query . "<br>\n"; //safe_query($query); } Quote Link to comment https://forums.phpfreaks.com/topic/263406-for-loop/#findComment-1357919 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.