stevedav Posted August 21, 2007 Share Posted August 21, 2007 greetings, I have a form with many checkboxes, representing different locations. i am storing them into an array locations[]. My question is how do I get them into MySQL? A foreach loop? I'm sure this is basic for most. My brain is frozen on it. Thanks. sd Quote Link to comment https://forums.phpfreaks.com/topic/65922-form-checkboxes-into-mysql/ Share on other sites More sharing options...
suma237 Posted August 21, 2007 Share Posted August 21, 2007 Try this <input type='checkbox' name="check1[]" value=<?=$row1['V_ProspectID'];?>></td> <!---CODE FOR STORING VALUE INTO DATABASE----> if (is_array($_POST["check1"])) { while (list($key,$value) = each($_POST["check1"])) { //echo "$key - $value<BR>"; $sql_update1="update ..... where V_ProspectID='$value'"; mysql_query($sql_update1)or die("Could not update table"); } } Quote Link to comment https://forums.phpfreaks.com/topic/65922-form-checkboxes-into-mysql/#findComment-329527 Share on other sites More sharing options...
vijayfreaks Posted August 21, 2007 Share Posted August 21, 2007 Hi.. you can store it in csv format in field value of sql. keep that field as text if u r having too much long text. or you will have array in get,post or request. so you can serialize it and store it in to database. @ the time of using you can deserialize it.. Regards, Vijay Quote Link to comment https://forums.phpfreaks.com/topic/65922-form-checkboxes-into-mysql/#findComment-329528 Share on other sites More sharing options...
cooldude832 Posted August 21, 2007 Share Posted August 21, 2007 I'm assuming your have a strucutre of <input type="checkbox" name="locations[]"> so lets do this <?php $string = ""; $i = 0; foreach($_POST['locations'] as $value){ //Sets all commas after first if($i != 0){echo ",";} //If not empty set the mysql bool to true if(!empty($value){ $string .= "Location ".$i." = 1"; } else{ $string .= "Location ".$i." = 0; } $i++; } echo $string; ?> That should produce a query string you can use assuming your sql table is structured like I have it written. Then just query it and you all set. If you are doing an insert it will be a tad different, but the idea is there A note on suma's code, while it will work, it will be a resource hog running so many queries Quote Link to comment https://forums.phpfreaks.com/topic/65922-form-checkboxes-into-mysql/#findComment-329529 Share on other sites More sharing options...
Barand Posted August 21, 2007 Share Posted August 21, 2007 Putting data in comma sep lists not a good design, especially if it is a list of foreign keys. To keep the data normalised, yet maintain a single query, use something like <?php if (isset($_GET['loc'])) { $id = $_GET['id']; foreach ($_GET['loc'] as $loc) { $sqlarr[] = "('$id', '$loc')"; } $sql = "INSERT INTO table VALUES\n" . join(",\n", $sqlarr); } echo "<pre>$sql</pre>"; // execute the query here. Echo for demo purposes only ?> <form> <input type="hidden" name="id" value="42"> <input type="checkbox" name="loc[]" value="1"> loc 1<br/> <input type="checkbox" name="loc[]" value="2"> loc 2<br/> <input type="checkbox" name="loc[]" value="3"> loc 3<br/> <input type="submit" name="sub" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/65922-form-checkboxes-into-mysql/#findComment-329569 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.