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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀