Every time I meet a guy named Adam, I ask him if he has a brother named Subtract'em. The name of this method is AddToDB. Once again, a corny name that stuck. Let's quickly discuss the logic behind what we are about to code. When an HTML form that is using the POST method is submitted, the field names and values are stored in the superglobal variable $_POST. When data from such a form is submitted, this AddToDB method should pull the column names from a table in the database, then compare those names with the names of the $_POST variables. For each name that matches, insert that name and value into two different arrays. Then we'll implode those arrays into as SQL statement that can be executed. If you are confused, then this code should help you out. First, we'll look at the method as a whole, and then break her up.
Don't panic! It looks like too much to handle at first, but if you take a closer look, it's really quite simple.
The AddToDB method accepts $tbl, which is the name of the database table into which we will execute our SQL statement.
$sql_colums - The array that will store all the column names from the database. $sql_columns_use - After we have compared the column names to the variable names, the names that matched will be stored in this array. $sql_value_use - Same as $sql_columns_use, but instead of the names, these are the values for each name.
We pull the column names from the database and put them into the $sql_columns array.
For each $_POST variable, see if its name ($key) exists in the $sql_columns array AND if it holds a value after white space is removed.
If the name of the variable matches one of the column names in $sql_columns, then we go through with adding its value to $sql_value_use and name to $sql_column_use. Notice how it checks to see if the value is set to "DATESTAMP," and if it is, then it uses the MySQL time stamp function. You can add as many of these conditions as you like, but this is one of the most common ones. If the value is a number, the single quotes aren't used, but if it's a string then it checks to see if magic quotes are turned on so we know whether to add slashes or not. This is pretty minimum security, so if you are a paranoid security nut - like me - then you will add quite a bit more validation. For now, validation before the variables get to this point is assumed. So now we have two arrays. One has column/variable names and the other has the values for each one of those names. Now all we have to do is slap them together and execute the statement.
First we check to see if the arrays have any values in them. If they don't, then that means that no column names matched the $_POST names so it returns an error.
Next we create the SQL statement by imploding the two arrays. Just think if you had to type all the columns and values out; that'd be unheard of. All that's left is to execute the statement. If it fails, then an error is returned. Two quickies: we need to create two small methods that can return any errors and the last SQL statement used.
Next we'll take a look at how we use our new class.
blog comments powered by Disqus |
|
|
|
|
|
|
|