【发布时间】:2012-08-14 17:32:00
【问题描述】:
以下代码应将数组中的每个键值对插入到表中的数学列值中。脚本不返回错误,但插入的行仅包含数组中的最后一个值
例如
array('one'=>1,'two'=>2,'three'=>3);
在具有第一、二和三列的表中成功插入行,但总共插入值 3。
$columns = array();
$bind = '';
foreach($array as $key => $value){
$columns[] = $key;
}
$columnString = implode($columns,',');
$valueString = implode($columns,',:');
$valueString = ':' . $valueString;
$core = core::getInstance();
$STH = $core->dbh->prepare("INSERT INTO table (" . $columnString . ") VALUES
(" . $valueString . ")");
foreach($array as $key => $value){
$STH->bindParam(':' . $key,$value);
}
【问题讨论】:
-
pdoStatement::bindParam将 对变量的引用 作为参数,pdoStatement::bindValue将 变量的值 作为参数。按照你的做法,你给了它对同一个变量$value的多个引用,当然这都会得到相同的值。