I have a PHP form with a structure like the following:
<input type="text" name="code[]" id="code[]" value="<?php echo $code[4] ?>" />
<input type="text" name="period[]" id="period[]" value="<?php echo $period[4]; ?>" />
There are five rows on my form with this structure. What I'm trying to do is, upon submit, ONLY insert records if the user enters data into these rows. Each row of input will produce a separate record. If the rows are blank, no record should be written. Right now, we have the INSERT statement in a for() loop, and it inserts blank records whenever the indexed field rows are empty. Here is what the INSERT statement looks like right now:
<?php
for($i=0; $i<=4; $i++) {
$new = $con->prepare("INSERT INTO table (code, period) VALUES(:code, :period)");
$data = array('code'=>$_POST['code'][$i],'period'=>$_POST['period'][$i]);
$new->execute($data);
}
?>
Is there a way to accomplish what I'm trying to do?