Solved

How can I insert multiple check box values into my database using php and mysql?

Posted on 2014-07-28
4
12,940 Views
Last Modified: 2014-07-29
I’m creating a employee training tracker... something that allows me to assign training items and retraining frequency for each user loaded in the database.  For a given user (user_id), I would like to display a dynamic list of training items (current items loaded in the database).  Then, using checkboxes, select the training items relevant to that user. For now, I just hardcoded the user_id (will change that later.  The form is submitting but nothing is hitting the database. Here is my code so far:
<?php

require_once 'scripts/app_config.php';
require_once 'scripts/database_connection.php';

//check for which boxes are selected then update database 
if(isset($_POST['$checkbox'])) { 
foreach($_POST['checkbox'] as $i) { 

$sql1="INSERT INTO user_training_items (item_id, user_id)VALUES('{$_POST['item_id'][$i]}', '{$_POST['user_id'][$i]}')";
$result1=mysql_query($sql1);
} 
} 
?> 
<form method="post" action="checkbox_form.php"> 
<?php
$sql="SELECT * FROM training_items";
$result=mysql_query($sql);

// Count table rows 
$count=mysql_num_rows($result);

$i = 0; 
while($rows=mysql_fetch_array($result)){ 
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox" type="checkbox" id="checkbox" value="<?php echo $i++;?>" /></td>
<td align="center"><? echo $rows['item_title'];?></td>
<input type="hidden" name="item_id" id="item_id" value="<? echo $rows['item_id'];?>"/>
<input type="hidden" name="user_id" id="user_id" value="1"/>
</tr>

<?php
}
?>

<input type="submit" name="Submit" value="Submit">
</form>

Open in new window


My tables are:

training_items
item_id
item_title


user_training_items
item_id
user_id
0
Comment
Question by:chaddcarr
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 200 total points
Comment Utility
This bit doesn't make sense:
if(isset($_POST['$checkbox'])) { 
foreach($_POST['checkbox'] as $i) { 

Open in new window

What is the field name? Is it '$checkbox' or 'checkbox'? Those are two different things.

Can you output the contents of $_POST by running:
print_r($_POST);

Open in new window

and copy and paste the data for us to see?
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 200 total points
Comment Utility
Also I should mention the input fields in your form (with type checkbox) should all have the same name that's an array, like 'mycheckbox[]' which should then be available to PHP in $_POST['mycheckbox'].
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
0
 

Author Comment

by:chaddcarr
Comment Utility
Thanks Terry,  incorporating both your comments, I ended up with the following code which works great.  I also added an } else { statement to clean it up a bit.  Thanks again!

<?php

require_once 'scripts/app_config.php';
require_once 'scripts/database_connection.php';

//check for which boxes are selected then update database 
if(isset($_POST['checkbox'])) { 
foreach($_POST['checkbox'] as $i) { 

$sql1="INSERT INTO user_training_items (item_id, user_id)VALUES('{$_POST['item_id'][$i]}', '{$_POST['user_id'][$i]}')";
$result1=mysql_query($sql1);
} 
echo "The records have been added to the user profile";
} else {

?> 
<form method="post" action="checkbox_form.php"> 
<?php
$sql="SELECT * FROM training_items";
$result=mysql_query($sql);

// Count table rows 
$count=mysql_num_rows($result);

$i = 0; 
while($rows=mysql_fetch_array($result)){ 
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $i++;?>" /></td>
<td align="center"><? echo $rows['item_title'];?></td>
<input type="hidden" name="item_id[]" id="item_id" value="<? echo $rows['item_id'];?>"/><br />
<input type="hidden" name="user_id[]" id="user_id" value="1"/>
</td>
</tr>

<?php
}
?>

<p><input type="submit" name="Submit" value="Add"></p>
</form>
<?php
}
?>

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 27
Best database setup for image uploads 6 38
populate dynamic drop down from a csv 3 34
Phone Dialer 5 35
Creating and Managing Databases with phpMyAdmin in cPanel.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now