troubleshooting Question

Inserting records from multiple sets of checkboxes and adding a text field into the mix

Avatar of Crazy Horse
Crazy HorseFlag for South Africa asked on
PHP
29 Comments2 Solutions262 ViewsLast Modified:
I had this question after viewing form validation - make sure at least 1 checkbox is selected.

I also require a text field as well as the already discussed checkbox inputs so the form would look something like:


text field input

checkbox 1 checkbox 2 checkbox 3

--------------------------------------------------------------

text field input

checkbox 1 checkbox 2 checkbox 3

--------------------------------------------------------------

text field input

checkbox 1 checkbox 2 checkbox 3


The fun part is trying to insert all of this into the database.

The way I have the table now (open to suggestions) is (fictitious names to illustrate what each column is)

ID      textFieldInput    CheckboxOption

 1         some text 1          checked1
 2         some text 1          checked2
 3         some text 2          checked1
 4         some text 2          checked2
 5         some text 2          checked3

So, this means that the user checked 2 checkboxes for text field one, and checked 3 options for text field 2. I hope that makes sense.

However, this is just the design. I can't actually get the data to insert into the database like that. I am using a foreach loop for the text field validation and a while loop for the checkbox validation. I don't know how to combine the two and perform the insert.

if(isset($_POST['submit']) && isset($_SESSION['times'])) {
		
		$message = "";
		
		foreach($_POST['unit_no'] as $unitNo) {
			if(empty($unitNo)) {
				
				$message .= "Unit number cannot be empty<br/>";
				break;
			}
			
		}
		
			$counter = 0;
		
		while($counter < $_SESSION['times']) {
			
			$counter++;
				 if (empty($_POST['defect'][$counter])) {
					 
					$message .= "Please select at least one option for unit " . $counter . "<br/>";
					 
			}
		}
		
		if($message) {
			
			echo error_message($message);
			
		} else {
			
			// loops here to insert records
			}
		}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 29 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 29 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros