Exemplo n.º 1
0
 /**
  * Insert the submitted form into the specified Mysql database.
  */
 public function run()
 {
     if (count($this->_fieldMap) == 0) {
         throw new Exception('Mysql success handler: field map is empty');
     }
     $link = @mysql_connect($this->_host, $this->_username, $this->_password);
     if (!$link) {
         throw new Exception('Mysql error: ' . mysql_error());
     }
     $db = @mysql_select_db($this->_dbname);
     if (!$db) {
         throw new Exception('Mysql error: ' . mysql_error());
     }
     $queryParts = array();
     $elements = $this->_form->getElements();
     foreach ($this->_fieldMap as $elementName => $fieldName) {
         if (array_key_exists($elementName, $elements)) {
             $element = $elements[$elementName];
             $value = $element->getValue();
             if (is_array($value)) {
                 $value = join(', ', $value);
             }
             $queryParts[] = "`{$fieldName}` = '" . mysql_real_escape_string($value) . "'";
         }
     }
     if (count($queryParts) == 0) {
         throw new Exception('Mysql success handler: query is empty');
     }
     if (strtolower($this->_form->getCharset()) == 'utf-8') {
         $utf8Query = "SET NAMES utf8";
         $utf8Result = @mysql_query($utf8Query);
         if (!$utf8Result) {
             throw new Exception('Mysql error: ' . mysql_error());
         }
     }
     $query = "INSERT INTO `{$this->_table}` SET " . join(', ', $queryParts);
     $result = @mysql_query($query);
     if (!$result) {
         throw new Exception('Mysql error: ' . mysql_error());
     }
     @mysql_close($link);
 }