authorId vs authorid

Somewhere around Chapter 6, the prepared statement for the insertJoke function went from :authorid to :authorId (with a cap I). The joke table does not have a column named authorId. I didn’t notice this until Chapter 8 when I copied the relevant files from the git repository and the website gave an error when visiting index.php?list. I changed line 32 in the jokeController.php file to $joke[‘authorid’] and now it works.

The name of the prepared statement has nothing to do with the name of the column. So you can write in the query

$query = "SELECT name FROM user WHERE user_id = :whatEverYouWant";

And set the prepared statement with

$stmt = bindValue("whatEverYouWant", 1);

But when you want the value out of the result you need to use the column name of course

$name = $result['name'];

So the name of the prepared statement and the column name are completly independent.

For clarity, the parameter name does not need to match the table column. What does need to match, however, are references to the pulled set of results, as PHP’s array keys are case sensitive.

Line 32 of jokeController tells the system to find an entry in the authors table where 'id' equals to a value "authorId" from the joke object, which is itself a result from the jokesTable, which at the end of Chapter 5 was created as "authorid" (Page 205).

The first reference I can find for Tom referring to authorId is page 243 (“Using Functions to Replace Queries”). The book seems to flicker back and forth between id and Id throughout chapter 6 and 7.

I see. The prepared statement did not cause the error. It was the missnamed array key.

Where is my mistake? The code works if I change ‘authorId’ to ‘authorid’ on line 32 of jokeController.php. Is it my mistake or is there a typo in the code? Did I miss a step somewhere?

I wouldnt say a step has been missed, however you may encounter this issue again in the future, as the book does seem to flipflop between id and Id, and i’m not sure if/where the code will make the wrong case reference again.