Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将循环值传输到另一个PHP表_Php_Mysql_Pdo - Fatal编程技术网

如何将循环值传输到另一个PHP表

如何将循环值传输到另一个PHP表,php,mysql,pdo,Php,Mysql,Pdo,我试图在提交按钮后将循环值转移到另一个表中 这是我的密码 $stmt = $db->prepare('Select * from productbottomtopstiches WHERE productsrfinformationID = :prodID'); $stmt->bindParam(':prodID', $srfid, PDO::PARAM_INT); $stmt->execute(); while($row = $stmt->fetch(PDO::FE

我试图在提交按钮后将循环值转移到另一个表中

这是我的密码

$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的值,但当我添加插入代码时,它只保存循环中的第一个值,第二个和第三个值丢失


有人能帮我修一下吗?谢谢。

您也可以将所有查询合并为一个查询。这也消除了获取的需要,然后循环第一个select查询并执行多个语句。但该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();
但无论出于何种原因,您仍然希望继续此路线,将第二个prepare移至外部并放入另一个容器中:

// 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
变量来准备循环中的第二个语句,覆盖了希望循环的第一个准备好的语句。这就是它在第一次插入后停止的原因。

您不需要在循环中准备查询。在循环之前准备一次,每次在循环中使用不同的值执行一次。或者,您可以只使用一个查询,将“prodID”绑定到一个insert-select查询。为什么会有不同的ID?ID只是
$srfid
,它不会改变。这是表格中唯一的一列吗?@Ghost我建议你在回答中演示如何做到这一点。哇!你最好!谢谢你帮助你,这是有史以来最好的鬼魂!你有没有我想注册的学校呵呵。我是php新手。再次感谢。@user5598179不,没有,也不打算再有一个哈哈:我很高兴这有帮助,请再有一个!哈哈。