dongyang7152
2014-03-07 13:36
浏览 197
已采纳

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.

图片转代码服务由CSDN问答提供 功能建议

我正在为我的工作室创建一个新的工作表程序表单。 我有一个工作表工作需要表格,有4个字段输入。 这些名为work1,work2,work3和work4。 它需要这些并写入数据库表jobcardwork。 这有两个领域,即作业卡和工作。 我希望它只在字段不为空时写入,因为可能并非所有3行都有文本。 我的代码是这样的。

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

这样可行,但即使包装盒中没有任何内容也会写入。 结果是。

  id jobcard work 
14 2 
15 2 linda 
16 2 dan 
   
 
 

as 你可以看到,它写了第一行并输入了作业卡号,但是框中没有文字,所以它留下了这个空值。 请帮忙。 谢谢。 p.s我知道mysql已经老了,但我开始在mysqli和PDO之前编写它。 对不起。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongqianzong4275 2014-03-07 13:42
    已采纳

    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

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题