I'm new to working with databases and am attempting to write my own handler for my databases. I have a PHP file containing a class with variables and functions to handle it. This file is then imported to the project needing to access the database in whatever way. In the project the data handler is created as an object then variables are set to tell the object the server name, user, password and database name. Once that data is defined then a function can be called with all the necessary data to build the query, including the table name and such.
I originally tried inserting all data as parameters IE:
$stm = $pdo->prepare("UPDATE ? SET ? = ? WHERE ? = ?");
However, this does not work so I end up with this:
$stm = $pdo->prepare("UPDATE $table SET $records = ? WHERE $compare = ?");
I process the $table, $records, and $compare variables so they only contain alpha-numerical characters, underscores and commas. Also I have set this file to where only the server can access it. I know that the whole purpose of the prepared statement is to prevent user input from entering the query string to fight injection, so is this considered safe practice? With the programming knowledge I already have this seems sound. The functions are inside a class so an attacker shouldn't be able to just call my include file and run their own input. The data is sent to this file from a server-side PHP script and is never touched by the user in any way.
Is this considered a safe practice? Or is there something else that I need to do in order to safe guard this, or just scrap this all together?
Clarification: In this article: (Can PHP PDO Statements accept the table or column name as parameter?) it states the reasoning for whitelisting the tables and columns is to prevent users from being able to input their own data. To the best of my knowledge I have done that. The only way I see a user being able to do so is for them to either upload a script to my server to access it, or on theirs but their server will not have rights to the file. On top of that, the file that holds this coding has no server or database information on it (ie: no username, password or database name). The way I see it, if they are able to use this script, they already have full access to my server and I have bigger concerns.