I'm just starting out using OOP; classes and methods etc. I've been looking around for some PDO classes/wrappers out there, but there's not much to choose from. So I've tried to make my own. Started out by writing an "insert" method.
As a man with low self esteem, I never like what I do myself, so I thought I'd ask you guys here for feedback. Here it is:
public function insert($tbl, $data) {
$this->_stmt = $this->_dbh->prepare("INSERT INTO $tbl (" . implode(', ', array_keys($data)) . ") VALUES (:" . implode(', :', array_keys($data)) . ")");
foreach($data as $key => $value) {
$this->_stmt->bindValue($key, $value, PDO::PARAM_STR);
}
$this->_stmt->execute();
}
The meaning was that it could all be done in one sweep instead of having several methods to do one thing; inserting a record.
An example of using this code:
$message = 'A message for the ones who like to read it!';
$sent_on = 'Saturday 23rd 2012';
$unique_id = 'unique_as_can_get';
$data = array(
'message' => $message,
'sent_on' => $sent_on,
'unique_id' => $unique_id
);
$insert = new DB;
$insert->insert('tablename', $data);
I've put the database connection into the constructor.
The code works as I want it to, but I'm still confused if it's "accepted" amongst you people who are way more skilled that I will ever be.
Please let me know if this code if "usable" and/or what is wrong with it, what can be improved/changed etc.