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 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测