Solved

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

Posted on 2014-07-28
4
13,120 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 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 110

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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