( This is a pretty lame table – there is no consideration to many MySQL best practices here, but it is only for demonstration purposes, so I hope you can forgive the quick and nasty implementation! )

The Create Method.

The create method will write a record to the database. This is ‘C’ in ‘CRUD’, a term you may have heard being thrown around. Can you guess what the other letters stand for? We’ll get to them in time… :)

But back to our Create. This is going to be a special create method. You’ll find lots of Create methods around the web that deal with adding one item to one field. All very good in theory, but not so great in practice. The method we are going to use here will allow you to add an array of values to a record – so you can populate a whole row in a MySQL table in one go, like proper Create methods should do.

So it may look a little complicated at first, but stick with it – we will go through it line by line.

The Breakdown!

A lot of code there doing lots of funky stuff. Let’s go through it line by line, as promised.

public function create($table, array $data){

No surprises here. Declaring a public function, so it can be accessed by the outside world, and we are passing the name of the table and an array of what we want added to that table. Let’s imagine we are adding a new snowboard to our database, the array that we will be passing to the Create Method would look something like this ( you don’t need to do anything with this yet… )

So now we get to building the query. At first we go through the array we have passed to the method, and convert the values into two strings that we can then use in our query string. If you need help on the foreach loops – look here. We are basically looping through the array we passed to the method and storing it as a string, with each item separated by commas. This is so we can pass it to the MySQL query.

$key_string contains the names of the keys in the array that equate to the names of the columns in our snowboard_stock table, and $value_string also contains the name of the columns, but with a prepended ‘:’ – this is so the PDO binding can use it later on ( if you need a refresher on PDO – check out this post ) .

Those of you who have had your espresso this morning may have noticed that our foreach loops would create two strings with trailing commas. This is a no-no as far as MySQL is concerned, so we need to remove them. The two lines above do just that.

What the hell is $this->dbh?! What the hell is prepare?! What the hell is ‘bindParam’?!

Whoah there, just glance back to the previous tutorial. $this->dbh was created when we instantiated our DB_handler object and is the PDO database connection we created. By assigning it to $this-> , it means that we can use this connection in any method in the class. Nifty eh?

The ‘prepare’ method is a PDO method that does some magic to protect our database against MySQL injection ( a post or two in itself. If you want to know more, look here. Otherwise, be satisfied that this is a good thing. )

The ‘bindParam’ method is a continuation of the PDO goodness. There is a more detailed post on this stuff back here, if you want to delve further.

$count = $stmt->execute();

The outcome of those previous lines leads us to here, we now have a bound PDO object ready to create our new record, and we do this with the ‘execute’ method, all safe in the knowledge that no MySQL injection baddies are going to get us.

We get the number of records that have been inserted returned from the execute method, very useful as we can tell whether we are a success ( $count > 0 ), or a miserable failure ( $count <= 0 ). $id = $this->dbh->lastInsertId();

We can then use the database handle, $this->dbh, to get the id of the last inserted record.