php - PDO Insert does not work -
i have problem can not make request prepared pdo every time returns false me.
$table = 'paroles'; $array = array( 'timestamp' => 'valeur', 'cle' => 'valeur', 'titre' => 'valeur', 'paroles' => 'valeur', 'titre_trad' => 'valeur', 'paroles_trad' => 'valeur' ); $values = '';$datas = ''; foreach ($array $key => $value) { $values = $values.$key.','; $datas = $datas.':'.$key.','; } $values = trim($values, ','); $datas = trim($datas, ','); $requestconstr = 'insert '.$table.'('.$values.') values('.$datas.')'; $sth = $this->_db->prepare($requestconstr); foreach ($array $key => $value) { $sth->bindvalue(':'.$key, $value); } if($sth->execute()){ return true; }else return false; // pdostatement object ( [querystring] => insert paroles(timestamp,cle,titre,paroles,titre_trad,paroles_trad) values(:timestamp,:cle,:titre,:paroles,:titre_trad,:paroles_trad) )
it looks building query properly, regardless, cleaner way it, , check @ end should give more information on going wrong:
$params = array_map(function($item) { return ":" . $item; }, array_keys($array)); $placeholders = implode(",", $params); $columns = implode(",", array_keys($array)); $sql = "insert {$table} ({$columns}) values ({$placeholders})"; $bind = array_combine($params, array_values($array)); $sth = $this->_db->prepare($sql); if ($sth->execute($bind)) { return true; } else { die(print_r($this->_db->errorinfo(), true)); }
Comments
Post a Comment