mikenl Posted June 25, 2008 Share Posted June 25, 2008 Hi, I have a checkbox array that I use to update a table row. Checkboxes look like: name=lang[] value=1 name=lang[] value=2 (etc) Table row looks like: id, someid lang1, 0 lang2, 1 lang3, 0 lang4, 0 lang5, 1 (etc) MySQL query create by code should be: UPDATE language SET lang1 = '1', lang2 = '1', lang3 = '0' (etc) WHERE id = 'someid' My code: $language = $_POST['lang']; // this is the checkbox array $setlanguage = "UPDATE prof_language SET "; foreach ($language as $key => $value){ if(!empty($key)){ $setlanguage = $setlanguage. ", "; } if($value){ $setlanguage = $setlanguage. "lang".$key." = '1'"; } else { $setlanguage = $setlanguage. "lang".$key." = '0'"; } } $setlanguage = $setlanguage. " WHERE unid = '$session_id'"; mysql_query($setlanguage); I have played with this for days but I can't get it to work. I need to update the row with new '1's and '0's... Link to comment https://forums.phpfreaks.com/topic/111796-solved-updating-table-row-with-checkbox-array/ Share on other sites More sharing options...
sasa Posted June 25, 2008 Share Posted June 25, 2008 try <?php $language = $_POST['lang']; // this is the checkbox array $setlanguage = "UPDATE prof_language SET "; for ($i = 1; $i <= 5; $i++){ $out[] = "lang$i = '".(in_array($i, $language) ? 1 : 0)."'"; } echo $setlanguage .= implode(', ', $out). " WHERE unid = '$session_id'"; mysql_query($setlanguage); ?> Link to comment https://forums.phpfreaks.com/topic/111796-solved-updating-table-row-with-checkbox-array/#findComment-574044 Share on other sites More sharing options...
mikenl Posted June 26, 2008 Author Share Posted June 26, 2008 works like charm thx! Link to comment https://forums.phpfreaks.com/topic/111796-solved-updating-table-row-with-checkbox-array/#findComment-575227 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.