perezf Posted May 23, 2008 Share Posted May 23, 2008 Hi guys i have these checkboxes that im trying to insert into a db separated by the pipe symbol, How can i do it? these are my checkboxes <input type="checkbox" name="interestlifestyles[]" value="1"> <input type="checkbox" name="interestlifestyles[]" value="2"> <input type="checkbox" name="interestlifestyles[]" value="3"> <input type="checkbox" name="interestlifestyles[]" value="4"> <input type="checkbox" name="interestlifestyles[]" value="5"> Link to comment https://forums.phpfreaks.com/topic/106997-inserting-items-into-db-from-checkbox/ Share on other sites More sharing options...
BlueSkyIS Posted May 23, 2008 Share Posted May 23, 2008 assuming the form is POST'ed: $in_value = implode('|',$_POST['interestlifestyles']); $sql = "INSERT INTO some_table (some_field) VALUES ('$in_value')"; Link to comment https://forums.phpfreaks.com/topic/106997-inserting-items-into-db-from-checkbox/#findComment-548435 Share on other sites More sharing options...
perezf Posted May 23, 2008 Author Share Posted May 23, 2008 Yes the form is posted I tried what you said and it only inserted the first item, but i selected multiple checkboxes but it didnt add them all just the first Link to comment https://forums.phpfreaks.com/topic/106997-inserting-items-into-db-from-checkbox/#findComment-548442 Share on other sites More sharing options...
BlueSkyIS Posted May 23, 2008 Share Posted May 23, 2008 you want all selected values separated by a pipe symbol, then inserted into a single field, correct? if so, this works for me: <?php if ($_SERVER['REQUEST_METHOD'] == "POST") { $in_value = implode('|',$_POST['interestlifestyles']); $sql = "INSERT INTO some_table (some_field) VALUES ('$in_value')"; echo $sql; } ?> <FORM METHOD='POST'> <input type="checkbox" name="interestlifestyles[]" value="1"> <input type="checkbox" name="interestlifestyles[]" value="2"> <input type="checkbox" name="interestlifestyles[]" value="3"> <input type="checkbox" name="interestlifestyles[]" value="4"> <input type="checkbox" name="interestlifestyles[]" value="5"> <INPUT TYPE='submit'> </FORM> output: INSERT INTO some_table (some_field) VALUES ('1|2|4') Link to comment https://forums.phpfreaks.com/topic/106997-inserting-items-into-db-from-checkbox/#findComment-548445 Share on other sites More sharing options...
perezf Posted May 23, 2008 Author Share Posted May 23, 2008 It is only inserting 1 of the checkboxes Link to comment https://forums.phpfreaks.com/topic/106997-inserting-items-into-db-from-checkbox/#findComment-548453 Share on other sites More sharing options...
BlueSkyIS Posted May 23, 2008 Share Posted May 23, 2008 did you run my code? what did the echo'd SQL look like? Link to comment https://forums.phpfreaks.com/topic/106997-inserting-items-into-db-from-checkbox/#findComment-548461 Share on other sites More sharing options...
johne90 Posted May 23, 2008 Share Posted May 23, 2008 I have had an issue with this before. I ended up just doing something like this: <?php foreach($_POST['interestlifestyles'] as $value){ $string="id='$field' or ".$string; } $string = substr($string, 0, -3); $sql = "INSERT INTO some_table $string"; ?> Link to comment https://forums.phpfreaks.com/topic/106997-inserting-items-into-db-from-checkbox/#findComment-548477 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.