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.