多行插入中的重复行

发布于 2024-10-22 00:29:04 字数 2035 浏览 1 评论 0原文

我正在尝试使用 PDO 向数据库中插入多行。我正在使用一个 if count(array_filter($value) > 0) 以防止插入空白行。下面的代码导致最后一行被插入两次。

有什么想法吗? 谢谢...

<td><input type="text" name="row[0][posnum]" value="123456"></td>  
         <td><input type="text" name="row[0][comment]" value="nancy"></td>  
         <td><input type="text" name="row[0][eff_dt]" value="12/12/2010"></td>  
        </tr>  
<tr>  
         <td><input type="text" name="row[1][posnum]" value="987654"></td>  
         <td><input type="text" name="row[1][comment]" value="bob"></td>  
         <td><input type="text" name="row[1][eff_dt]" value="01/01/2011"></td>  
         </tr>  
<tr>  
         <td><input type="text" name="row[2][posnum]"></td>  
         <td><input type="text" name="row[2][comment]"></td>  
         <td><input type="text" name="row[2][eff_dt]"></td>  
         </tr>

PDO 插入代码

$arr = $_POST["row"];   
       # connect to the database  
    try {  
      $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);  
      $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  

  //code in question      
   foreach($arr as $key=>$value){

            # the data we want to insert  

        if(count(array_filter($value)) >0){

           $data = array( 'submitby' => $_POST['submitby'], 'posnum' => $value['posnum'], 'comment' => $value['comment'], 'eff_dt' => date("Y-m-d",
           strtotime($value['eff_dt'])) );  

       }

  $STH = $DBH->prepare("INSERT INTO table (submitby, POS_NUM, COMMENT,EFF_DT) value (:submitby, :posnum, :comment, :eff_dt)");  
            $STH->execute($data);  


           }

}  //end try  
    catch(PDOException $e) {  
        echo "I'm sorry, Dave. I'm afraid I can't do that.";  
        echo $e->getMessage();  
    }  

}

I'm trying to insert into my database multiple rows using PDO. I'm using an
if count(array_filter($value) >0) to prevent blank rows from being inserted. The code below causes the last row to be inserted twice.

Any ideas why?
Thanks...

<td><input type="text" name="row[0][posnum]" value="123456"></td>  
         <td><input type="text" name="row[0][comment]" value="nancy"></td>  
         <td><input type="text" name="row[0][eff_dt]" value="12/12/2010"></td>  
        </tr>  
<tr>  
         <td><input type="text" name="row[1][posnum]" value="987654"></td>  
         <td><input type="text" name="row[1][comment]" value="bob"></td>  
         <td><input type="text" name="row[1][eff_dt]" value="01/01/2011"></td>  
         </tr>  
<tr>  
         <td><input type="text" name="row[2][posnum]"></td>  
         <td><input type="text" name="row[2][comment]"></td>  
         <td><input type="text" name="row[2][eff_dt]"></td>  
         </tr>

PDO Insert Code

$arr = $_POST["row"];   
       # connect to the database  
    try {  
      $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);  
      $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  

  //code in question      
   foreach($arr as $key=>$value){

            # the data we want to insert  

        if(count(array_filter($value)) >0){

           $data = array( 'submitby' => $_POST['submitby'], 'posnum' => $value['posnum'], 'comment' => $value['comment'], 'eff_dt' => date("Y-m-d",
           strtotime($value['eff_dt'])) );  

       }

  $STH = $DBH->prepare("INSERT INTO table (submitby, POS_NUM, COMMENT,EFF_DT) value (:submitby, :posnum, :comment, :eff_dt)");  
            $STH->execute($data);  


           }

}  //end try  
    catch(PDOException $e) {  
        echo "I'm sorry, Dave. I'm afraid I can't do that.";  
        echo $e->getMessage();  
    }  

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莳間冲淡了誓言ζ 2024-10-29 00:29:04

你不是说,也许这就是逻辑...

$arr = $_POST["row"];
# connect to the database
try {
    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    //code in question
    foreach($arr as $key=>$value){
        # the data we want to insert  

        if(count(array_filter($value)) >0){

           $data = array( 'submitby' => $_POST['submitby'], 'posnum' => $value['posnum'], 'comment' => $value['comment'], 'eff_dt' => date("Y-m-d",
           strtotime($value['eff_dt'])) );  

           $STH = $DBH->prepare("INSERT INTO table (submitby, POS_NUM, COMMENT,EFF_DT) value (:submitby, :posnum, :comment, :eff_dt)");
           $STH->execute($data); 
        }
    }
} //end try
   catch(PDOException $e) {
   echo "I'm sorry, Dave. I'm afraid I can't do that.";
   echo $e->getMessage();
}

Didn't You mean, maybe it's the logic...

$arr = $_POST["row"];
# connect to the database
try {
    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    //code in question
    foreach($arr as $key=>$value){
        # the data we want to insert  

        if(count(array_filter($value)) >0){

           $data = array( 'submitby' => $_POST['submitby'], 'posnum' => $value['posnum'], 'comment' => $value['comment'], 'eff_dt' => date("Y-m-d",
           strtotime($value['eff_dt'])) );  

           $STH = $DBH->prepare("INSERT INTO table (submitby, POS_NUM, COMMENT,EFF_DT) value (:submitby, :posnum, :comment, :eff_dt)");
           $STH->execute($data); 
        }
    }
} //end try
   catch(PDOException $e) {
   echo "I'm sorry, Dave. I'm afraid I can't do that.";
   echo $e->getMessage();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文