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

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.

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?