$_POST checkboxes as mysql INSERT INTO

I’m trying to figure out a loop to get the values from checkboxes that have been checked within my form to be inserted into my MySQL table along with the profileid. I want to know what the best method would be to do this.

At the end of the code I want “Profile id - roofing”, “profile id - Flooring” input into my table; according to what has been checked.

This is what I’ve written. It doesn’t work. I don’t really know why. I’m still a novice at all this and would be grateful for an expert’s help on the most efficient and best practice.

[php]	
$profileid = mysql_prep($_GET['profileid']);

//Interests Array
$interests = array('Remodeling', 'Plumbing', 'Carpentry', 'Electrical', 'Roofing', 'Flooring', 'Masonry/Concrete', 'Landscaping/Yard Work', 'Painting', 'House Cleaning', 'Children', 'Pregnant Teens', 'Single Mothers', 'Battered Women', 'Seniors', 'Homeless', 'Hungry/Food Pantry', 'Community Garden', 'Clerical Assistance', 'Meals on Wheels Delivery', 'Big Bro/Big Sis', 'Prison Ministry', 'Hair Cutting', 'Legal Assistance', 'Dental Care', 'Medical Care', 'Outreach Trips(Domestic)', 'Outreach Trips(Global)');

if (isset($_POST['submit'])) { // Form has been submitted.
	for($i=0; $i<count.($interests); $i++){
			$interest = $_POST[$i];
			$query = "INSERT INTO interests (profileid, interest) VALUES ($profileid, $interest)";
			$result = mysql_query($query, $connection) or die(mysql_error());
		
		if($result){
			redirect_to("volunteer.php");
		}
	}
} 

[/php]


[php] include("includes/header.php");[/php]

			<div class="FormContainer">
			[php] echo "<form action='addinterests.php?=" . $profileid . "' enctype='multipart/form-data' method='post'>";[/php]
				<h2>Areas of Interest: (Check ALL that apply)</h2><br>
				<div class="Form-t">
				[php]
				echo "<table cellpadding='7' align='center'>";
					$i=0;
					while ($i<count($interests)){
						echo "<tr>";
						$r=0;
						for ($r=0; $r<2; $r++){
							echo "<td><input type='checkbox' name='" . $i . "' value='" . $interests[$i] . "'/>" . $interests[$i] . "</td>";
							$i++;
						}
						echo "</tr>";
					}
				echo "</table>";
				[/php]
				</div>
			</div>
			<div class="ButtonHolder">
				<center>
				<input type="submit" name="submit" value="Submit" />
				</form>
				</center>
			</div>
			
[php] include("includes/footer.php");[/php]

Thank you in advance for taking the time to help me.

Well, Josh,

To read a posted checkbox variable, you handle it like any other posted form part.

First, each checkbox has a separate name. Therefore, you have to get their values and
then you can deal with the format you need to build to get them into your database.

To read a posted checkbox you handle it this way:

if (isset($_POST[‘sport’])) {
echo “sport option checked…”;
}

You can place all of your “NAMES” of your checkboxes into an array and then loop thru them
checking for them being checked. So, you are sort of close. But, your FOR loop actually checks
for just the $i int that counts thru your interests, not the actual interests! So, you have to change
your FOR loop to pull the interest NAME out of it and test that one.
Sooo, do not check if (isset($_POST[‘1’]) But, if(isset($_POST[‘remodeling’]) ALSO, make sure your names of the checkboxes are spelled exactly correct. (CAPS do count!)

Hope that makes sense…

Thanks for the help. This is what I ended up with and it works great. Now on to other problems. :slight_smile:

[code][php]
if (isset($_POST[‘submit’])) { // Form has been submitted.
$interestlist = array_values($_POST);
array_pop($interestlist);
foreach($interestlist as $key => $value){

		$query = 	"INSERT INTO 
					interests (profileid, interest) 
					VALUES ('{$profileid}', '{$value}'
					)";
		$result = mysql_query($query, $connection) or die(mysql_error());
}
if($result){
	redirect_to("profile.php?id=" . $profileid);
}

}
[/php][/code]

Sponsor our Newsletter | Privacy Policy | Terms of Service