Saving values from check boxes into data base.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mwphurst
Forum Newbie
Posts: 17
Joined: Fri Jan 16, 2004 9:59 pm

Saving values from check boxes into data base.

Post by mwphurst »

I am having problems saving all the boxes checked into my data base. I am pulling a list of check box values from a data base, then trying to get the boxes that are checked put back into a different table. I am able to get the form to add the last one checked, but I can't get multiple entries. I have never been very good at getting info into arrays. I assume that's what I need to do, is get all the values from the checked boxes into an array, then do a foreach and add them to a table.

Here's what I have so far. It isn't much.
while($row = mysql_fetch_array($result)){
$value_id=$row["value_id"];
$value_name= $row["value_name"];
echo"<input type=\"checkbox\" value=\"$value_name\" name=\"group_value\">$value_name";
}
$query = "INSERT INTO ".$prefix."store_options_".$group." set value='$group_value'";
$result = mysql_query($query) or die("Error: " . mysql_error());
What do I need to get all the group_values?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

First the checkboxes:

Code: Select all

<?php

while ($row = mysql_fetch_assoc($result)) {
	// you don't really need temporary variables for variables you only
	// use once

	// you need to change the name of the checkboxes to group_value[] so
	// that all the selected options will be entered into an array

	// surely you want the value to be the id and not the name?
	echo '<input type="checkbox" value="'.$row['value_id'].'" name="group_value[]"> '.$row['value_name'];
}

?>
Do each of the values go into a separate row in the other table or what?

Mac
mwphurst
Forum Newbie
Posts: 17
Joined: Fri Jan 16, 2004 9:59 pm

Post by mwphurst »

Thanks twigletmac.

I found the info on adding the [] after the group_name last night to make them into an array. I can't believe it was that easy. Well, actually I can. =)
Do each of the values go into a separate row in the other table or what?
Yes they are. That's why I want to use the name rather than the id.
User avatar
wmasterj
Forum Commoner
Posts: 40
Joined: Mon Aug 18, 2003 5:52 pm
Location: Stockholm, Sweden

Post by wmasterj »

Thx twiggie - that was xtremly helpfull. ;)
Post Reply