From: Thomas Mudrunka Date: Sat, 6 Aug 2011 05:22:04 +0000 (+0200) Subject: REPLACE INTO nahrazeno vyrazem UPDATE - ON DUPLICATE KEY (kvuli referencni integrite) X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=aa3fd0a8f66965bde175c23631937ba4667248a0;p=mirrors%2FSokoMan.git REPLACE INTO nahrazeno vyrazem UPDATE - ON DUPLICATE KEY (kvuli referencni integrite) --- diff --git a/index.php b/index.php index 7d500ba..0614a8d 100755 --- a/index.php +++ b/index.php @@ -349,7 +349,8 @@ class Sklad_DB extends PDO { $table = $this->escape($table); //Get list of POSTed columns - $columns = implode(',',array_map(array($this,'escape'), array_keys($values[0]))); + $columns_array = array_map(array($this,'escape'), array_keys($values[0])); + $columns = implode(',',$columns_array); //Build query $sql = ''; @@ -372,8 +373,7 @@ class Sklad_DB extends PDO { } //Insert into table (columns) - $sql .= $replace ? 'REPLACE' : 'INSERT'; - $sql .= " INTO $table ($columns) VALUES "; + $sql .= "INSERT INTO $table ($columns) VALUES "; //Values (a,b,c),(d,e,f) $comma=''; @@ -399,6 +399,15 @@ class Sklad_DB extends PDO { $comma = ','; } + //On duplicate key + if($replace) { + foreach($columns_array as $col) { + if($col == $table.'_id' || $col == $table.'_valid_till') continue; + $on_duplicate[] = "$col=VALUES($col)"; + } + $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate); + } + //Terminate $sql .= ';'; return $sql;