update table using dynamic prepared statements

浪子不回头ぞ 提交于 2019-12-03 22:28:55

Move this line inside the second foreach loop

$out1 .= $column." = :$column, ";

But the result is awkward for the current data,since it will generate something like

...SET description = :description,description = :description....

Solved!

I had to unset($out1); after $output is written and execute the $out1 line in the second foreach loop (see code below).

$out = "UPDATE `foo` SET ";
foreach($data as $itemId=>$value) {
    foreach($value as $column=>$foo) {
        $out1 .= $column." = :$column, ";
    }
    $out2 = "WHERE id = :id";

    $output .= $out.rtrim($out1, ", ")." ".$out2."<br>";
    unset($out1);
}

echo $output;

The next step will be to prepare the statement using $db->prepare($output); and pass the data to $db->execute($data);.
So to answer my own question - yes, dynamic prepared statements using one "global" array IS possible ! :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!