多行插入中的重复行
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不是说,也许这就是逻辑...
Didn't You mean, maybe it's the logic...