jarv Posted August 4, 2011 Share Posted August 4, 2011 Please help, I have 6 checkboxes in an array 0|1|2|3|4|5 I need to be able to UPDATE my database based on which checkboxes are checked, if it's the last one then I don't want to add a leading '|' here is my code so far: if(sizeof($_POST['hear'])) { foreach($_POST['hear'] AS $heard){ $heard = $heard.'|'; if($heard == '5'){ $heard = $heard; } } //end foreach } //end IF $sql1 = "UPDATE aarbookts_booking_bookings SET how='$heard' WHERE id = '$id'"; $result1 = mysql_query($sql1,$link) or die('Error: ' . mysql_error() . '<br>SQL: ' . $sql1); Please help?! Quote Link to comment Share on other sites More sharing options...
requinix Posted August 4, 2011 Share Posted August 4, 2011 What do those numbers mean? Are they related to something else in the database already? Quote Link to comment Share on other sites More sharing options...
jarv Posted August 4, 2011 Author Share Posted August 4, 2011 Database column is ServiceDue values can be: 0|4 2|3|5 0|2|3 or whatever?! everything would be 0|1|2|3|4|5 Quote Link to comment Share on other sites More sharing options...
jarv Posted August 4, 2011 Author Share Posted August 4, 2011 the numbers are the values of the checkboxes Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 4, 2011 Share Posted August 4, 2011 You *should* ideally be using an associative table with individual records for each value. Alternatively you could use a binary value where each bit represents a 0 or 1. E.g. the number "13 is equivalent to the binary value of "01101" which would represent 5=false, 4=true, 3=true, 2=false, 1=true. This gives you the ability to query the records based upon certain values being set or not using bit-wise operators. However, if you really are determine to store multiple values as a single string, then you simply need to use implode. Replace all of this foreach($_POST['hear'] AS $heard){ $heard = $heard.'|'; if($heard == '5'){ $heard = $heard; } } //end foreach With just this $heard = implode('|', $_POST['hear']); Quote Link to comment Share on other sites More sharing options...
jarv Posted August 5, 2011 Author Share Posted August 5, 2011 WOW that's brilliant! how such small piece of code can do so much?! Quote Link to comment 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.