php - Cannot insert into MySQL database using PDO....No errors -
i have problem cannot insert mysql database using pdo.
i no errors whenever check database if row has been inserted, table empty.
i know have connection database able select not insert.
here class entends pdo
class database extends pdo { public function __construct($db_type, $db_host, $db_name, $db_user, $db_pass) { parent::__construct($db_type.':host='.$db_host.';dbname='.$db_name, $db_user, $db_pass); //parent::setattribute(pdo::attr_errmode, pdo::errmode_exceptions); } /** * select * @param string $sql sql string * @param array $array paramters bind * @param constant $fetchmode pdo fetch mode * @return mixed */ public function select($sql, $array = array(), $fetchmode = pdo::fetch_assoc) { $sth = $this->prepare($sql); foreach ($array $key => $value) { $sth->bindvalue("$key", $value); } $sth->execute(); return $sth->fetchall($fetchmode); } /** * insert * @param string $table name of table insert * @param string $data associative array */ public function insert($table, $data) { /*ksort($data); $fieldnames = implode('`, `', array_keys($data)); $fieldvalues = ':' . implode(', :', array_keys($data)); $sth = $this->prepare("insert $table (`$fieldnames`) values ($fieldvalues)"); foreach ($data $key => $value) { $sth->bindvalue(":$key", $value); }*/ $sth = $this->prepare("insert user (`login`, `password`, `role`) values (:login, :password, :role)"); $sth->bindvalue(':login', 'username'); $sth->bindvalue(':password', 'password'); $sth->bindvalue(':role', 'owner'); $sth->execute(); /*if ($sth->errorcode() != 0) { $arr = $sth->errorinfo(); throw new exception('sql failure:'.$arr[0].':'.$arr[1].':'.$arr[2]); }*/ $sth->debugdumpparams(); }
here table structure (kept simple debugging).
create table if not exists `user` ( `userid` int(11) not null auto_increment, `login` varchar(25) not null, `password` varchar(64) not null, `role` enum('default','admin','owner') not null default 'default', primary key (`userid`) ) engine=innodb
edit:
i found out problem lies. problem data array. if use $data['first_name'] when calling insert() function, fails if replace $data[] values hard codes values, works problem lies $data[]
i create array of post variables
// post data registration form $data = array(); $data['first_name'] = $_post['first_name']; $data['last_name'] = $_post['last_name']; $data['gender'] = $_post['gender']; $data['email'] = $_post['email']; $data['interests'] = $_post['interests']; $data['username'] = $_post['username']; $data['password'] = $_post['password']; $data['newsletter'] = $_post['newsletter']; $data['terms'] = $_post['terms']; $data['captcha'] = $_post['captcha'];
this gets passed create function
public function create($data) { $this->db->insert('users', array( 'first_name' => $data['first_name'], //this won't work 'first_name' => 'whatever name is', //this work 'last_name' => $data['last_name'], 'email' => $data['email'], 'username' => $data['username'], 'password' => $password, 'user_salt' => $user_salt, 'sex' => $data['gender'], 'interests' => $data['interests'], 'signup_date' => date('y-m-d h:i:s'), 'verification_code' => $code ));
and gets inserted
public function insert($table, $data) { ksort($data); $fieldnames = implode('`, `', array_keys($data)); $fieldvalues = ':' . implode(', :', array_keys($data)); $sth = $this->prepare("insert $table (`$fieldnames`) values ($fieldvalues)"); foreach ($data $key => $value) { $sth->bindvalue(":$key", $value); } $sth->execute(); }
$fieldvalues = implode(':,', array_keys($data));
Comments
Post a Comment