html多个输入字段

I am creating a new form for my jobsheet program for my workshop. I have a jobsheet work required form with 4 fields to input. These are named work1, work2, work3 and work4. It takes these and writes to the database table jobcardwork. This has two fields, jobcard and work. I want it to write only if the field is not empty as maybe not all 3 lines have text. My code is like this.

$a = array($work1, $work2, $work3, $work4); 

 foreach ($a as $b) 

 { 

 $sql6 = "INSERT INTO jobcardwork (Jobcard,Work)VALUES('2','$b')";
 $result6=mysql_query($sql6); 

 }

This works but will write even if there is nothing in the box. The result is.

id      jobcard   work
14      2    
15      2         linda
16      2         dan

as you can see, its written the first line and entered the jobcard number but there was no text in the box so it has left this null. Please help. Thanks. p.s I know mysql is old but I started writting it before mysqli and PDO. Sorry.

Solution without iffing:

$a = array($work1, $work2, $work3, $work4); 
foreach (array_filter($a) as $b) { 
   $sql6 = "INSERT INTO jobcardwork (Jobcard,Work)VALUES('2','$b')";
   $result6=mysql_query($sql6); 
}

See http://lt1.php.net/array_filter

Your code could be much more optimized and written more safe, but it is not the topic of this question, so your quick fix would look like this:

$a = array($work1, $work2, $work3, $work4); 
foreach ($a as $b) { 
  if($b!="") {
     $sql6 = "INSERT INTO jobcardwork (Jobcard,Work)VALUES('2','$b')";
     $result6=mysql_query($sql6); 
  }
}

The right way is to remove extra spaces, get rid of sql attacks and then check if text is still empty.

Check mysql_real_escape_string and trim method. Final code will look like this.

 $result = array();
 foreach ($a as $b) 
 { 
    $b = mysql_real_escape_string(trim($b));

    if($b != '')
    {
        $sql = "INSERT INTO jobcardwork (Jobcard,Work)VALUES('2','$b')";
        $result[] = mysql_query($sql); 
    }

 }
 var_dump($result);