如何将循环值传递给另一个表PHP

时间:2021-07-17 03:50:01

I am trying to transfer my loop values in another table after submit button

我试图在提交按钮后在另一个表中传输我的循环值

Here's my code

这是我的代码

$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 ));
 }

The values from productbottomtopstiches are 3

productbottomtopstiches的值为3

It displays 3 values different ID, Code, Color and TKT but when i added the insert code it only save the first value in the loop the 2nd and 3rd value is missing..

它显示3个不同的ID,代码,颜色和TKT值,但是当我添加插入代码时,它只保存循环中的第一个值,缺少第2个和第3个值。

can someone help me to fix it? Thanks.

有人可以帮我解决吗?谢谢。

1 个解决方案

#1


2  

As an alternative, you can just join the queries altogether into one. This also takes out the need of fetching, then looping the first select query and executing multiple statements. But the binding of that ID will still be there:

作为替代方案,您可以将查询完全合并为一个。这也需要获取,然后循环第一个选择查询并执行多个语句。但该ID的绑定仍然存在:

Example:

例:

$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();

But for whatever reason, you still want to continue this route, move the second prepare outside and into another container:

但无论出于何种原因,你仍然希望继续这条路线,将第二个准备工作转移到另一个容器中:

// 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
    ));
}

And in case you're wondering the reason why is it just inserting once is because, you used the same $stmt variable into preparing into the second inside the loop, overwriting the first prepared statement that you wished to be looped. That's why it stopped after the first insertion.

如果您想知道为什么它只是插入一次是因为,您使用相同的$ stmt变量准备进入循环内的第二个,覆盖您希望循环的第一个准备好的语句。这就是它在第一次插入后停止的原因。

#1


2  

As an alternative, you can just join the queries altogether into one. This also takes out the need of fetching, then looping the first select query and executing multiple statements. But the binding of that ID will still be there:

作为替代方案,您可以将查询完全合并为一个。这也需要获取,然后循环第一个选择查询并执行多个语句。但该ID的绑定仍然存在:

Example:

例:

$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();

But for whatever reason, you still want to continue this route, move the second prepare outside and into another container:

但无论出于何种原因,你仍然希望继续这条路线,将第二个准备工作转移到另一个容器中:

// 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
    ));
}

And in case you're wondering the reason why is it just inserting once is because, you used the same $stmt variable into preparing into the second inside the loop, overwriting the first prepared statement that you wished to be looped. That's why it stopped after the first insertion.

如果您想知道为什么它只是插入一次是因为,您使用相同的$ stmt变量准备进入循环内的第二个,覆盖您希望循环的第一个准备好的语句。这就是它在第一次插入后停止的原因。