Solved

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

Posted on 2014-07-28
4
12,979 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
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 200 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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40226557
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now