Now we create functions to insert and update data. Insert and update share a lot of code, so we will combine them into a third function which will be transparent to outside users.
/*
* InsertQuery
* Desc: Insert data into the database.
* Parms:
* $tableName - database table name.
* $values - associative array of field names and corresponding values.
* $debug - If true then return SQL query without executing.
* Returns:
* Nothing on success.
* Error String on failure.
*/
Function InsertQuery ($tableName, $values, $debug=false)
{
/* Insert the $values into the database.
* e.g.
* $values = array ("name"=>"kris","email"=>"[email protected]");
* InsertQuery ("employee", $values);
*/
return InsertUpdateQuery ("", $tableName, $values, $debug);
/*
* UpdateQuery
* Desc: Update data in the database.
* Parms:
* $tableName - database table name.
* $values - associative array of field names and corresponding values.
* $where - SQL Where clause to specify which row(s) to update.
* $debug - If true then return SQL query without executing.
* Returns:
* Nothing on success.
* Error String on failure.
*/
Function UpdateQuery ($tableName, $values, $where="", $debug=false)
{
/* Update the $values in the database.
* e.g.
* $values = array ("name"=>"kris","email"=>"[email protected]");
* $where = "WHERE id='1'";
* UpdateQuery ("employee", $values, $where);
*/
if (empty($where)) $where = " ";
return InsertUpdateQuery ($where, $tableName, $values, $debug);
// If you do not want to add quotes
// around the field then specify
// /*NO_QUOTES*/ when passing in the value.
// For update statements like
// "update poll set total_votes=total_votes+1",
// you do not want
// the value field to have quotes around it.
if (strstr($val,"/*NO_QUOTES*/")){