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 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件