Dragen Posted May 24, 2007 Share Posted May 24, 2007 Okay, I've got an array which outputs like this using print_r() Array ( [mon] => mon = '1', [tues] => tues = '1', [weds] => weds = '1', [thurs] => thurs = '1', [fri] => fri = '1', ) Each value is the day name then = '1'.. it's sometime 0, but that shouldn't matter at all. I'm trying to set this up to be in a mysql statement. At the moment my mysql statement looks like this: $sqlup = "UPDATE staff SET mon = '" . $sday['mon'] . "', tues = '" . $sday['tues'] . "', weds = '" . $sday['weds'] . "', thurs = '" . $sday['thurs'] . "', fri = '" . $sday['fri'] . "', `desc` = '" . $desc . "' WHERE firstname = '" . $name['0'] . "' && surname = '" . $name['1'] . "'"; But I'm wanting to dynamically change all of the days with the values of my array. Is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/52826-solved-using-an-array-in-mysql/ Share on other sites More sharing options...
marmite Posted May 24, 2007 Share Posted May 24, 2007 Not quite sure what you want to do. You want to update the values of the fields 'mon', 'tues' etc with the 1s and 0s you've stored in your array? You already seem to be doing this in your SQL query? Or you want to only update those fields held in the array, so if you don't have a 'weds' value, you don't want to update it? I'm certain you can make your query more dynamic by using an array, just not sure what you want to achieve. http://uk3.php.net/manual/en/function.array-search.php might help. Quote Link to comment https://forums.phpfreaks.com/topic/52826-solved-using-an-array-in-mysql/#findComment-260902 Share on other sites More sharing options...
chigley Posted May 24, 2007 Share Posted May 24, 2007 <?php $array = array("field1" => "value1", "field2" => "value2"); $q = "UPDATE table SET "; foreach($array as $field => $value) { $q .= "'$field' = '$value', "; } ?> Not tested, but you get the idea Quote Link to comment https://forums.phpfreaks.com/topic/52826-solved-using-an-array-in-mysql/#findComment-260924 Share on other sites More sharing options...
taith Posted May 24, 2007 Share Posted May 24, 2007 Array ( [mon] => '1', [tues] => '1', [weds] => '1', [thurs] => '1', [fri] => '1', ); $sqlup = "UPDATE staff SET mon = '" . $sday['mon'] . "', tues = '" . $sday['tues'] . "', weds = '" . $sday['weds'] . "', thurs = '" . $sday['thurs'] . "', fri = '" . $sday['fri'] . "', `desc` = '" . $desc . "' WHERE firstname = '" . $name['0'] . "' && surname = '" . $name['1'] . "'"; my only question is... if your just having it come from the array every time... why not put the #s in manually? Quote Link to comment https://forums.phpfreaks.com/topic/52826-solved-using-an-array-in-mysql/#findComment-260932 Share on other sites More sharing options...
Dragen Posted May 24, 2007 Author Share Posted May 24, 2007 thanks for all the replies! Thanks also to Chigly who sorted the problem I never realised that I could 'add on' to a variable by using '.=' instead of just '='. That's solved the whole thing! Thanks note: the reason I don't want to do it manually is because the array is something which could change frequently so instead of editing all the mysql statements each time I can just change the one array and it does it all for me. Quote Link to comment https://forums.phpfreaks.com/topic/52826-solved-using-an-array-in-mysql/#findComment-261091 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.