doupingpeng7567
doupingpeng7567
2017-02-10 07:04
浏览 45
已采纳

插入具有条件的多行

I'm inserting a multiple row data with foreach() loop. But I don't want to insert a particular row if its value is tax or discount. How do I do that.

my code:

<?php
if(isset($_POST['finalsave']))
{
  foreach ($_POST['descr'] as $key => $descr)
  {
    $dept = mysql_real_escape_string($_POST['category']);
    $idate = mysql_real_escape_string($_POST['idate']);
    $desc1 = mysql_real_escape_string($descr);
    $tags = mysql_real_escape_string($_POST['name'][$key]);
    $location = mysql_real_escape_string($_POST['location'][$key]);
    $war = mysql_real_escape_string($_POST['war'][$key]);
    $qty = mysql_real_escape_string($_POST['qty'][$key]);
    $rate = mysql_real_escape_string($_POST['rate'][$key]);
    $finalamt = mysql_real_escape_string($_POST['finalamt'][$key]);

    $sql2 = ("INSERT INTO `project`.`master_inventory` (`category`, `idate`, `descr`, `name`, `location`,  `war`, `qty`, `rate`, `finalamt`)
      VALUES ('".$dept."','".$idate."','".$desc1."','".$tags."','".$location."','".$war."','".$qty."','".$rate."','".$finalamt."')");
    $sqlins = mysql_query($sql2);
    //echo $sql2;
  }
}
?>

图片转代码服务由CSDN问答提供 功能建议

我正在使用foreach()循环插入多行数据。 但是如果它的值是税或折扣,我不想插入特定的行。 我该怎么做。

我的代码:

 &lt;?php 
if(isset($ _ POST ['finalsave']  ))
 {
 foreach($ _POST ['descr'] as $ key =&gt; $ descr)
 {
 $ dept = mysql_real_escape_string($ _ POST ['category']); 
 $ idate = mysql_real_escape_string  ($ _POST ['idate']); 
 $ desc1 = mysql_real_escape_string($ descr); 
 $ tags = mysql_real_escape_string($ _ POST ['name'] [$ key]); 
 $ location = mysql_real_escape_string($ _ POST  ['location'] [$ key]); 
 $ war = mysql_real_escape_string($ _ POST ['war'] [$ key]); 
 $ qty = mysql_real_escape_string($ _ POST ['qty'] [$ key])  ; 
 $ rate = mysql_real_escape_string($ _ POST ['rate'] [$ key]); 
 $ finalamt = mysql_real_escape_string($ _ POST ['finalamt'] [$ key]); 
 
 $ sql2 =(“  INSERT INTO`project``master_inventory`(`category`,`idate`,`descr`,`name`,`location`,`war`,`qty`,`rate`,`finalamt`)
 VALUES(  ' “$部。”', ' “$ IDATE。”' ' “$ DESC1。”' ' “$标签。”' ' “$位置。”''”。$战 。“','”。$ qty。“','”。$ rate。“','”。$ finalamt。“')”); 
 $ sqlins = mysql_quer  y($ sql2); 
 // echo $ sql2; 
} 
} 
?&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongyi6543
    dongyi6543 2017-02-10 07:13
    已采纳

    Hope this will helps you.

     foreach ($_POST['descr'] as $key => $descr)
      {
        if(($descr !="tax")||($descr !="discount"))
        {
           //Write your logic here
        }
    }
    
    点赞 评论
  • duansha6410
    duansha6410 2017-02-10 07:11

    Check whether the value is tax or discount before insert query.

    As you said,descr field in table contains the value tax or discount which is assigned to variable $desc1

    <?php
    if(isset($_POST['finalsave']))
    {
      foreach ($_POST['descr'] as $key => $descr)
      {
        $dept = mysql_real_escape_string($_POST['category']);
        $idate = mysql_real_escape_string($_POST['idate']);
        //this variable $desc1 will have the value tax or discount
        $desc1 = mysql_real_escape_string($descr);
        $tags = mysql_real_escape_string($_POST['name'][$key]);
        $location = mysql_real_escape_string($_POST['location'][$key]);
        $war = mysql_real_escape_string($_POST['war'][$key]);
        $qty = mysql_real_escape_string($_POST['qty'][$key]);
        $rate = mysql_real_escape_string($_POST['rate'][$key]);
        $finalamt = mysql_real_escape_string($_POST['finalamt'][$key]);
        //inserts only if $desc1 is not tax or discount
        if(($desc1!="tax")||($desc1!="discount")
        {
        $sql2 = ("INSERT INTO `project`.`master_inventory` (`category`, `idate`, `descr`, `name`, `location`,  `war`, `qty`, `rate`, `finalamt`)
              VALUES ('".$dept."','".$idate."','".$desc1."','".$tags."','".$location."','".$war."','".$qty."','".$rate."','".$finalamt."')");
            $sqlins = mysql_query($sql2);
        }
    }
    }
    ?>
    
    点赞 评论
  • doushi9729
    doushi9729 2017-02-10 07:12

    Put if condition in foreach loop like this:

    <?php
    if(isset($_POST['finalsave']))
    {
      foreach ($_POST['descr'] as $key => $descr) {
      // this you have to do
        if(($descr != 'tax') || ($descr != 'discount'))
        {
          $dept = mysql_real_escape_string($_POST['category']);
          $idate = mysql_real_escape_string($_POST['idate']);
          $desc1 = mysql_real_escape_string($descr);
          $tags = mysql_real_escape_string($_POST['name'][$key]);
          $location = mysql_real_escape_string($_POST['location'][$key]);
          $war = mysql_real_escape_string($_POST['war'][$key]);
          $qty = mysql_real_escape_string($_POST['qty'][$key]);
          $rate = mysql_real_escape_string($_POST['rate'][$key]);
          $finalamt = mysql_real_escape_string($_POST['finalamt'][$key]);
    
          $sql2 = ("INSERT INTO `project`.`master_inventory` (`category`, `idate`, `descr`, `name`, `location`,  `war`, `qty`, `rate`, `finalamt`)
            VALUES ('".$dept."','".$idate."','".$desc1."','".$tags."','".$location."','".$war."','".$qty."','".$rate."','".$finalamt."')");
          $sqlins = mysql_query($sql2);
            //echo $sql2;
        }
      }
    }
    ?>
    
    点赞 评论

相关推荐