Solved

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

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

808 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