The following code should insert each key-value pair in the array into the value of the mathematical column in the table. the script does not return errors, but the inserted row contains only the last value in the array
eg.
array('one'=>1,'two'=>2,'three'=>3);
insert row successfully into table with columns one, two and three, but insert value 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); }
source share