且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP生成动态PDO插入

更新时间:2023-01-30 17:18:08

忘了bindParam,只需使用execute并将值$array传递给它即可:

Forget about bindParam, just use execute and pass it the values of $array:

$STH->execute($array);

或者,您可以完全擦除命名的参数,以简化您的代码:

Alternatively, you could scratch the named parameters altogether to simplify your code a little:

$columnString = implode(',', array_keys($array));
$valueString = implode(',', array_fill(0, count($array), '?'));

$STH = $core->dbh->prepare("INSERT INTO table ({$columnString}) VALUES ({$valueString})");
$STH->execute(array_values($array));