douhuang5331 2018-09-16 03: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-16 17: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 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
  • ¥15 IEC61850 客户端和服务端的通讯机制
  • ¥15 MAX98357a(关键词-播放音频)
  • ¥15 Linux误删文件,请求帮助
  • ¥15 IBMP550小型机使用串口登录操作系统
  • ¥15 关于#python#的问题:现已知七自由度机器人的DH参数,利用DH参数求解机器人的逆运动学解目前使用的PSO算法
  • ¥15 发那科机器人与设备通讯配置