?
Solved

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

Posted on 2014-07-28
4
Medium Priority
?
13,177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 800 total points
ID: 40225924
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 800 total points
ID: 40225928
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
 

Author Comment

by:chaddcarr
ID: 40227447
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

801 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