Inserting checkbox values into database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dedurus
Forum Newbie
Posts: 7
Joined: Thu Sep 20, 2007 10:53 pm

Inserting checkbox values into database

Post by dedurus »

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:

Code: Select all

 
<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());
    }
    
}
 
 
?>



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.


I hope someone can help me solve this or give me some guideline.

Thanks in advance.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Inserting checkbox values into database

Post by pcoder »

Try this.It will help you to identify the post value.

Code: Select all

 
print_r($_POST);
 
dedurus
Forum Newbie
Posts: 7
Joined: Thu Sep 20, 2007 10:53 pm

Re: Inserting checkbox values into database

Post by dedurus »

thanks pcoder.
i've tried printing the $_POST array and all the values are correct.
I need a loop which will insert all checked values in the database
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Inserting checkbox values into database

Post by aceconcepts »

Code: Select all

 
for($x=0; $x<count($_POST['field']); $x++)
{
//get you fields by $_POST['field'][$x]
}
 
dedurus
Forum Newbie
Posts: 7
Joined: Thu Sep 20, 2007 10:53 pm

Re: Inserting checkbox values into database

Post by dedurus »

thanks aceconcepts, that was completely helpful and solve my problem.
thanks again.
Post Reply