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 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到