October 27, 200817 yr Hi to all I need help for this problem that i'm trying to solve for a while (i'm new in PHP). I have a form with several checkboxes which values are pulled from a database. I managed to display them in the form, assign an appropriate value to each, but cannot insert their values into other database. Here's the code: <form id="form1" name="form1" method="post" action=""> <?php $info_id=$_GET['info_id']; $kv_dodatoci=mysql_query("SELECT * FROM `dodatoci`") or die('ERROR DISPLAYING: '.mysql_error()); while($kol=mysql_fetch_array($kv_dodatoci)){ $id_dodatoci=$kol['id_dodatoci']; $mk=$kol['mk']; echo '<input type="checkbox" name="id_dodatoci[]" id="id_dodatoci" value="'.$id_dodatoci.'" />'; echo '<label for="'.$id_dodatoci.'">'.$mk.'</label><br />'; } ?> <input type="hidden" value="<?=$info_id?>" name="info_id" /> <input name="insert_info" type="submit" value="Insert Additional info" /> </form> <?php if(isset($_POST['insert_info']) && is_array($id_dodatoci)){ echo $id_dodatoci.'<br />'; echo $mk.'<br />'; // --- Guess here's the problem ----- // foreach($_POST['id_dodatoci'] as $dodatok){ $dodatok_kv=mysql_query("INSERT INTO `dodatoci_hotel`(id_dodatoci,info_id) VALUES ('$dodatok','$info_id')") or die('ERROR INSERTING: '.mysql_error()); } } ?> I need to insert a new record in the database for each checked checkbox, so i guess the problem lies in the foreach loop. I hope someone can help me solve this or give me some guideline. Thanks in advance.
October 27, 200817 yr Hi to all I need help for this problem that i'm trying to solve for a while (i'm new in PHP). I have a form with several checkboxes which values are pulled from a database. I managed to display them in the form, assign an appropriate value to each, but cannot insert their values into other database. Here's the code: <form id="form1" name="form1" method="post" action=""> <?php $info_id=$_GET['info_id']; $kv_dodatoci=mysql_query("SELECT * FROM `dodatoci`") or die('ERROR DISPLAYING: '.mysql_error()); while($kol=mysql_fetch_array($kv_dodatoci)){ $id_dodatoci=$kol['id_dodatoci']; $mk=$kol['mk']; echo '<input type="checkbox" name="id_dodatoci[]" id="id_dodatoci" value="'.$id_dodatoci.'" />'; echo '<label for="'.$id_dodatoci.'">'.$mk.'</label><br />'; } ?> <input type="hidden" value="<?=$info_id?>" name="info_id" /> <input name="insert_info" type="submit" value="Insert Additional info" /> </form> <?php if(isset($_POST['insert_info']) && is_array($id_dodatoci)){ echo $id_dodatoci.'<br />'; echo $mk.'<br />'; // --- Guess here's the problem ----- // foreach($_POST['id_dodatoci'] as $dodatok){ $dodatok_kv=mysql_query("INSERT INTO `dodatoci_hotel`(id_dodatoci,info_id) VALUES ('$dodatok','$info_id')") or die('ERROR INSERTING: '.mysql_error()); } } ?> I need to insert a new record in the database for each checked checkbox, so i guess the problem lies in the foreach loop. I hope someone can help me solve this or give me some guideline. Thanks in advance. Hi i was only working on this idea last week for a future project try this <html> <head></head> <body> <?php include "connect.php"; if(!isset($_POST['submit'])) { ?> <?php print "<form action='test.php' method='post'>"; print "Name\n\r<input type='text' name='name' ><br>"; $query=mysql_query("SELECT * from access"); while($result=mysql_fetch_array($query, MYSQL_ASSOC)) { $access = array("$result[active]"); sort($access); foreach($access as $a) print("<input type='checkbox' name='access[]' value='$a' >$a<br>"); } print("<input type='submit' name='submit' value='Select'>"); print("</form>"); } elseif(is_array($_POST['access'])) { print "You selected: <br />"; $name=$_POST['name']; foreach($_POST['access'] as $a ) { $insert="insert into block (active,name) values ('$a','$name')"; mysql_query($insert) or die ("could not insert"); print "$a <br>"; } } else print("Nothing selected."); ?> </body> </html> just change the table name and the variables to your requirments
October 28, 200817 yr Author thanks for the tip ELITE. my problem is to loop through all checkboxes, and for each checked, populate a separate record in a database. actually i don't know how to recognize the which box is checked, and put the appropriate value in db. thanks again
Create an account or sign in to comment