Example #1
0
 /** Save the current set of keys and values to the database.  If this object is new (has no 'id' set), 
  * an 'insert' query is done, and the resulting 'id' is set in the object.  If the object is not new, then
  * an 'update' query is done. */
 public function save()
 {
     /* We need to prepend the name of the database to all entries */
     $name = $this->getName();
     if ($this->isNew()) {
         /* Grab the keys and values from the array of data entries */
         $arrKeys = array_keys($this->arrData);
         $arrValues = array_values($this->arrData);
         /* Format the keys properly */
         foreach ($arrKeys as &$key) {
             if (!clsDB::isValidFieldName($key)) {
                 throw new Exception(ERRORMSG_INVALID);
             }
             $key = strtolower($key);
             $key = "`{$name}_{$key}`";
         }
         /* Format the values properly */
         foreach ($arrValues as &$value) {
             $value = mysql_real_escape_string($value);
             $value = "'{$value}'";
         }
         $strKeys = implode(',', $arrKeys);
         $strValues = implode(',', $arrValues);
         $SQL = "INSERT INTO `<<tbl><{$name}>>` ({$strKeys}) VALUES ({$strValues});";
         $strID = clsDB::insertQuery($SQL);
         $this->setInteger('id', $strID);
     } else {
         $arrSets = array();
         foreach ($this->arrData as $key => $value) {
             $key = strtolower($key);
             $value = mysql_real_escape_string($value);
             $arrSets[] = "`<<{$name}><{$key}>>`='{$value}'";
         }
         $strSets = implode(',', $arrSets);
         $strID = $this->get('id');
         $SQL = "UPDATE `<<tbl><{$name}>>` SET {$strSets} WHERE `<<{$name}><id>>`='{$strID}';";
         clsDB::query($SQL);
     }
 }