douhuang5331 2018-09-16 11:30
浏览 84
已采纳

如何在使用PHP插入到db之前检查任何输入值是否有数据?

Insertion is happening when html table row is empty.if any of the input field has value allow to insert otherwise dont insert blank row to database.Where i need to change my code?MY datas are inserting if table row of datas are null

php code

    for ($count = 0; $count < count($itemname); $count++) {
        $itemname_clean  = mysqli_real_escape_string($link, $itemname[$count]);
        $unit_clean      = (int) $unit[$count];
        $quantity_clean  = (int) $quantity[$count];
        $rate_clean      = $rate[$count];
        $totamount_clean = $totamount[$count];
        $checkvat_clean  = $checkvat[$count];
        if ($itemname_clean != '' || $unit_clean != '' || $quantity_clean != '' || $rate_clean != '' || $totamount_clean != '') {
            $query .= '
   //insert query goes here
        }
    }
  • 写回答

1条回答 默认 最新

  • dongzan2740 2018-09-17 01:49
    关注

    So you want to check if any one of the variables has a value, then insert. But if all of the variables are empty, don't insert.

    You have a mistake in your if statement. You are checking $totamount which is an array so it is not empty, so the if statement always resulted in true. But inside, you are inserting $totamount_clean to the database which might be empty.

    So change $totamount to $totamount_clean in your if statement.

    if (!empty($itemname_clean) || !empty($unit_clean) || !empty($quantity_clean) || !empty($rate_clean) || !empty($totamount_clean)) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵