php - My code is not inserting custom object -
i working pdo, want insert using feature can provide custom object/class parameters isnert. no error thrown in catch block. if try raw insert entering fields table works fine.
here custom class want create instance of , insert using pdo
class user { public $email; public $username; public $fname; public $sname; function __construct($e,$u,$fn,$sn) { $this->email = $e; $this->username = $u; $this->fname = $fn; $this->sname = $sn; } }
here code use try , insert
try{ $dbh = new pdo("mysql:host=localhost;dbname=mydb", "myuser", "mypass"); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); $user = new user('the email','the username','the fname','the sname'); $sth = $dbh->("insert user (email, username, fname, sname) value (:email, :username, :fname, :sname)"); $sth->execute((array)$user); } catch(pdoexception $e) { echo $e->getmessage(); }
you want bind values first , call execute. http://www.php.net//manual/en/pdostatement.bindparam.php
example
$sth = $dbh->prepare("insert user (email, username, fname, sname) value (:email, :username, :fname, :sname)"); $sth->bindparam(':email', $user->email); // , on $sth->execute();
Comments
Post a Comment