gammaman Posted April 29, 2008 Share Posted April 29, 2008 I need help figuring out why my query is not updating the way it should. foreach($Grade as $itm =>$value) { print_r($value); mysql_query("Update Rcourse set Grade = '$value' where Grade < 'A' AND StudentID='$itm' AND CourseID='{$_SESSION['checkaddGrades']['course']}'") or die(mysql_error()); } # end of foreach Here is the array you are seeing from the foreach Array ( [1] => A ) Array ( [311] => C+ ) I want the Grade to be set to either A or C+ depeding upon the studentID which is the key inside the []. When I run it assigns the word array to just the first occurance of the query. Link to comment https://forums.phpfreaks.com/topic/103399-solved-help-with-basic-query/ Share on other sites More sharing options...
rhodesa Posted April 29, 2008 Share Posted April 29, 2008 before the foreach, do a print_r($Grade); and post what that is...your array looks a bit weird Link to comment https://forums.phpfreaks.com/topic/103399-solved-help-with-basic-query/#findComment-529515 Share on other sites More sharing options...
gammaman Posted April 29, 2008 Author Share Posted April 29, 2008 This is what the Grade array has before the foreach loop Array ( [0] => Array ( [1] => A ) [1] => Array ( [311] => C+ ) ) Link to comment https://forums.phpfreaks.com/topic/103399-solved-help-with-basic-query/#findComment-529517 Share on other sites More sharing options...
rhodesa Posted April 29, 2008 Share Posted April 29, 2008 Yeah, that is what I thought it would be...it's just oddly structured...this should work for you though: <?php foreach($Grade as $set){ foreach($set as $itm =>$value){ print "Giving student $itm grade $value\n"; mysql_query("Update Rcourse set Grade = '$value' where Grade < 'A' AND StudentID='$itm' AND CourseID='{$_SESSION['checkaddGrades']['course']}'") or die(mysql_error()); } } ?> Not sure why you have "WHERE Grade < 'A'" either...but that is how you can get your array values properly Link to comment https://forums.phpfreaks.com/topic/103399-solved-help-with-basic-query/#findComment-529522 Share on other sites More sharing options...
rhodesa Posted April 29, 2008 Share Posted April 29, 2008 Also, in my opinion, the array should look like: Array ( [1] => A [311] => C+ ) Then your original code would work Link to comment https://forums.phpfreaks.com/topic/103399-solved-help-with-basic-query/#findComment-529525 Share on other sites More sharing options...
gammaman Posted April 29, 2008 Author Share Posted April 29, 2008 Thanks a million that worked perfectly. Link to comment https://forums.phpfreaks.com/topic/103399-solved-help-with-basic-query/#findComment-529528 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.