What you’re trying to achieve can be achieved using various approaches.
But first I’ll start with why your current code isn’t working. First of all, you didn’t define a parameter for the function. If a function has something passed to it (above you’ve passed $select) then it should have a way of handling that, e.g.:
function products($action){
Secondly, variables are variable. When you call the products function on your bottom line, you pass $select as a variable. But $select hasn’t been given a value.
Thirdly, when you use the = operator, it means ‘assign’. So basically, $Name = ‘Bob’ would mean that everywhere you use $Name afterwards, unless it is changed again, it’s the same thing as putting ‘Bob’. So the four lines in the function are assigning $select to the return of a string, which doesn’t make more sense. What you are trying to do is change the outcome of a function based on what’s given to it. I’ll show you how in a moment.
The fourth major thing is that $productsName and $productID aren’t set as anything, so the function would attempt to plug values in which it doesn’t have.
So, here’s my suggestion based on your code above. There are other methods but they might be a little tricky to grasp without knowing about functions.
There are a couple of things I could suggest. The first is using constants. Define constants that have a unique value. The best method with this approach, because it wouldn’t make sense to use an addition of multiple values, is to use consecutive numbers:
define('SELECT', 1);
define('INSERT', 2);
define('UPDATE', 3);
define('DELETE', 4);
The 2 benefits of using constants here are that they always hold the same value as long as they have been previously defined and that they are available everywhere in the application. This is different from variables because at different places in the application they can have different values and, therefore, functions can’t use them without them being passed to them because they wouldn’t know which instance of the variable they are supposed to be using. Constants are written in uppercase to make it obvious that they are constants and not a typo which should have been a variable or a string.
Next stage is to write a function which is given a parameter. Essentially this parameter holds a number, defined above, which can be compared with the above constants. So if the number 1 (which is now equal to the constant SELECT) is passed to it, it will return the select query etc. The second parameter will be optional and will hold the name or ID if expected:
function products($Action, $Parameter = false){ //If you set a value in the parameter, it will be that value unless something has been passed
if($Parameter !== false){
$Parameter = mysql_real_escape_string($Parameter); //ALWAYS escape your data to prevent MySQL injection. If you're using a different database system than MySQL, use the equivalent function.
}
switch($Action){ //the switch statement is like an if/or but with one variable with multiple possibilities.
case SELECT:
return "SELECT * FROM Products";
break;
case INSERT:
if($Parameter === false){ //if no parameter is given, error out.
throw new Exception("No product name given to the INSERT query");
}else{
return "INSERT INTO Products SET ProductsName='{$Parameter}'";
}
break;
case UPDATE:
if($Parameter === false){
throw new Exception("No product ID given to the UPDATE query");
}else{
return "UPDATE ProductsName FROM Products WHERE ProductID='{$Parameter}'";
}
break;
case DELETE:
if($Parameter === false){
throw new Exception("No product ID given to the DELETE query");
}else{
return "DELETE * FROM Products WHERE ProductID='{$Parameter}'";
}
break;
}
return false; //An unknown action has been passed
}
That function may include a lot of new stuff so read the comments and the PHP manual if you’re unsure on anything.
From then, all you need to do is make calls to the function like the following:
products(SELECT); //returns the select query string
products(INSERT); //errors out because you haven't given a name
products(INSERT, 'someproduct'); //returns the insert query to insert a product with the name 'someproduct'
products(UPDATE, 1); //returns the beginning of the update query string for product #1
products(DELETE, 1); //returns the query string to delete product #1
If you’re no happy with using constants, then replace the constants with strings, such as products(“Select”), making changes to the switch() statement to look for strings not constants.
Hope I didn’t go through it too fast for you there, if you have any problems or questions just ask ahead 