gamefreak13 Posted August 23, 2007 Share Posted August 23, 2007 I have 4 different checkboxes on a form that adds some data to my MySQL database. The problem, is only one of the 4 checkboxes will work at any given time. If someone checks Checkbox 1 and Checkbox 3, the database field says 'Checkbox 3'. I was expecting to see 'Checkbox 1Checkbox 3', but it doesn't merge them. What I want to do, is if a user selects Checkbox 1 and Checkbox 3, for the 'result' to be merge into 'Checkbox 1, Checkbox 2' with the comma. I don't know if this would be a php array or a foreach or what. I know it's really simple though and I've probably passed over the code before. I will also need to trunicate the last 2 characters no matter what, because of the comma and space. I don't want it showing up as 'Checkbox 1, Checkbox 3, '. <input type="checkbox" name="type" value="Checkbox 1"> Checkbox 1<br> <input type="checkbox" name="type" value="Checkbox 2"> Checkbox 2<br> <input type="checkbox" name="type" value="Checkbox 3"> Checkbox 3<br> <input type="checkbox" name="type" value="Checkbox 4"> Checkbox 4 The 'type' field gets inserted into mysql table. Quote Link to comment https://forums.phpfreaks.com/topic/66378-solved-forming-a-sql-statement-from-multiple-checkboxes/ Share on other sites More sharing options...
MadTechie Posted August 23, 2007 Share Posted August 23, 2007 erm.. first thats not valid for a form, try <input name="checkbox4" type="checkbox" value="Checkbox 4"/>Checkbox 4 Quote Link to comment https://forums.phpfreaks.com/topic/66378-solved-forming-a-sql-statement-from-multiple-checkboxes/#findComment-332189 Share on other sites More sharing options...
gamefreak13 Posted August 24, 2007 Author Share Posted August 24, 2007 Do you mean like this? (I set this up so I could set the vars via the URL to keep things simple) There has to be a MUCH cleaner way of doing this. I want it to output 'Checkbox1, Checkbox2' or just 'Checkbox1' or just 'Checkbox2' depending on what the person checks. test2.php?checkbox1=aaa&checkbox2=aaa <? $checkbox1 = $_GET['checkbox1']; // We have register_globals off so we need this $checkbox2 = $_GET['checkbox2']; // We have register_globals off so we need this if($checkbox1) { $querypt1 = "Checkbox1, "; } // If it is set then output 'Checkbox1, ' if($checkbox2) { $querypt2 = "Checkbox2, "; } // If it is set then output 'Checkbox2, ' $final = "$querypt1$querypt2"; // Merge everything $query = substr($final,0,-2); // Removes last two characters of the entire thing which are a comma and a space echo $query; ?> Quote Link to comment https://forums.phpfreaks.com/topic/66378-solved-forming-a-sql-statement-from-multiple-checkboxes/#findComment-332613 Share on other sites More sharing options...
gamefreak13 Posted August 24, 2007 Author Share Posted August 24, 2007 The end result is whatever the output is, is to go in to a MySQL statement, and thus there can be no ', ' (comma then a space) at the end. I just think this is an extremely sloppy way of doing this. I am certain there is a much simpler/cleaner way of doing this. Quote Link to comment https://forums.phpfreaks.com/topic/66378-solved-forming-a-sql-statement-from-multiple-checkboxes/#findComment-332621 Share on other sites More sharing options...
gamefreak13 Posted August 24, 2007 Author Share Posted August 24, 2007 I think this is what I am looking for, but don't see a need to serialize then unserialize.. they cancel each other out.. why do it at all? http://www.evolt.org/node/60222 Quote Link to comment https://forums.phpfreaks.com/topic/66378-solved-forming-a-sql-statement-from-multiple-checkboxes/#findComment-332624 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 what about this, (something to play with) i hope it makes sense <form method="post" enctype="application/x-www-form-urlencoded" name="myform"> Bike<br /> On road <input name="bike" type="radio" value="on" checked="checked" /> <br /> Off road <input name="bike" type="radio" value="off" /> <br /> <br /> CAR<br /> <input type="checkbox" name="car[]" value="on" /> on road <input type="checkbox" name="car[]" value="off" /> off road <br /> <br /> <input name="submit" type="submit" value="ok" /> </form> <?php $car = implode(",", $_POST['car']); echo "bike:"; echo $_POST['bike']; echo "<br /><br />"; echo "car:"; echo $car; ?> Quote Link to comment https://forums.phpfreaks.com/topic/66378-solved-forming-a-sql-statement-from-multiple-checkboxes/#findComment-332628 Share on other sites More sharing options...
gamefreak13 Posted August 24, 2007 Author Share Posted August 24, 2007 I will play with that.. I think the IMPLODE is what I was thinking of.. I'll return with my findings.. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/66378-solved-forming-a-sql-statement-from-multiple-checkboxes/#findComment-332629 Share on other sites More sharing options...
corbin Posted August 24, 2007 Share Posted August 24, 2007 I would possibly get ghetto with it.... Something like the following if I didn't feel like hand coding quite a bit: <?php if(isset($_POST['submit'])) { $wheres = array( 1 => 'table.column = \'value\'', 2 => 'other_table.column = \'other value\'', ); $checks = (isset($_POST['checkbox'])) ? $_POST['checkbox'] : array(); $where = array(); foreach($checks as $v) { if(!is_numeric($v)) continue; if(isset($wheres[$v])) { $where[] = $wheres[$v]; } $query = 'SELECT * FROM table, other_table'; if(count($where) > 0) { $query .= 'WHERE '; $query .= implode(' AND ', $where); } $q = mysql_query($query); } } else { //output a form with a submit button named 'submit' and check boxes named 'checkbox[]' } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66378-solved-forming-a-sql-statement-from-multiple-checkboxes/#findComment-332632 Share on other sites More sharing options...
gamefreak13 Posted August 24, 2007 Author Share Posted August 24, 2007 Thanks corbin, but I was looking for just a simple bit of code. What you provided looks rather confusing (arrays are greek to me), and MadTechie's code did the job. Thanks MadTechie! I cleaned it up to the bare essentials and it works great! <form method="post"> <input type="checkbox" name="type[]" value="On Road"> On Road<br> <input type="checkbox" name="type[]" value="Off Road"> Off Road<br><br> <input name="submit" type="submit" value="ok"> </form> <?php $type = implode(", ", $_POST['type']); echo $type; ?> Quote Link to comment https://forums.phpfreaks.com/topic/66378-solved-forming-a-sql-statement-from-multiple-checkboxes/#findComment-332641 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 Coolie, i gave you a few options to play with, hoped something would prove useful Quote Link to comment https://forums.phpfreaks.com/topic/66378-solved-forming-a-sql-statement-from-multiple-checkboxes/#findComment-332642 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.