Automation Is Good
Much of what we do when writing web applications is overhead. A decent amount of code in any web application is going to be written out of sheer necessity because of a previous action. This extra code isn't productive, it's not enjoyable and each time you have to write it you open up the possibility to human error. Thankfully there are ways to severely limit the amount of impact this mundane overhead has.
Throughout this guide we're going to look at a specific scenario. We will address the issue and we'll solve it accordingly. We'll be doing our testing through whatever medium you're most comfortable with. I'm in my terminal, doing things from the command line. You may feel free to use phpMyAdmin if that's your choice.
The scenario: You run a site with a membership system. Articles posted to your site can be commented on; only members may comment. And naturally you won't to keep track of how many comments each user has. You can do this any number of ways but the two most common are:
Either count the comments table, looking for rows where the user ID of the comment is the same as the profile being viewed. Or store the number of comments the user has in the users table. I prefer the latter and it's what this guide is going to be written off. Number of comments is a detail belonging to the member, so it might as well be stored with the rest of their details in the user table.