我试图在提交按钮
后在另一个表中传输我的循环值这是我的代码
$stmt = $db->prepare('Select * from productbottomtopstiches WHERE
productsrfinformationID = :prodID');
$stmt->bindParam(':prodID', $srfid, PDO::PARAM_INT);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$topstichescode= $row['topstichescode'];
$color = $row['color'];
$topstichestkt = $row['topstichestkt'];
$stmt = $db->prepare("INSERT INTO productpifbottomtopstiches(
productpifinformationID,
topstichescode,
color,
topstichestkt
)
VALUES(
:pid,
:code,
:color,
:tkt
)");
$stmt->execute(array(
':pid' => $srfid,
':code' => $topstichescode,
':color' => $color,
':tkt' => $topstichestkt ));
}
productbottomtopstiches的值为3
它显示3个不同的ID,代码,颜色和TKT值,但是当我添加插入代码时,它只保存循环中的第一个值,缺少第2个和第3个值。
有人可以帮我解决吗?感谢。
答案 0 :(得分:2)
作为替代方案,您可以将查询完全合并为一个。这也需要获取,然后循环第一个选择查询并执行多个语句。但该ID的绑定仍然存在:
示例:
$stmt = $db->prepare('
INSERT INTO productpifbottomtopstiches(
productpifinformationID,
topstichescode,
color,
topstichestkt
)
SELECT productsrfinformationID, topstichescode, color, topstichestkt FROM
productbottomtopstiches WHERE productsrfinformationID = :prodID
');
$stmt->bindParam(':prodID', $srfid, PDO::PARAM_INT);
$stmt->execute();
但无论出于何种原因,你仍然希望继续这条路线,将第二个准备工作转移到另一个容器中:
// first statement
$stmt = $db->prepare('Select * from productbottomtopstiches WHERE
productsrfinformationID = :prodID');
$stmt->bindParam(':prodID', $srfid, PDO::PARAM_INT);
$stmt->execute();
// second statement
$stmt2 = $db->prepare("INSERT INTO productpifbottomtopstiches(
productpifinformationID,
topstichescode,
color,
topstichestkt
)
VALUES(
:pid,
:code,
:color,
:tkt
)");
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$topstichescode = $row['topstichescode'];
$color = $row['color'];
$topstichestkt = $row['topstichestkt'];
// execute second statement
$stmt2->execute(array(
':pid' => $srfid,
':code' => $topstichescode,
':color' => $color,
':tkt' => $topstichestkt
));
}
如果您想知道为什么它只插入一次是因为,您使用相同的$stmt
变量准备进入循环内的第二个,覆盖您希望的第一个准备好的语句循环这就是为什么它在第一次插入后停止的原因。