fhiiqm/inc/func_lib.inc.php
changeset 20 5877137431e4
parent 19 33ee23fafd4d
child 21 97dba0fe820c
--- a/fhiiqm/inc/func_lib.inc.php	Fri Dec 09 14:33:22 2011 +0100
+++ b/fhiiqm/inc/func_lib.inc.php	Thu Dec 15 16:42:56 2011 +0100
@@ -205,5 +205,44 @@
             </html>\n";
         }    
     }
+    
+    function duplicate_record ($table, $key_name, $key_value) 
+    {
+    	include_once($_SERVER['DOCUMENT_ROOT'] ."/fhiiqm/inc/dbconnect.inc.php");
+        global $dbc; // Verbindung ist schon da? 
+    	if (!isset($dbc) || !$dbc) $dbc = new dbconnection();
+
+        // Get the names of all fields/columns in the table
+        $sql = 'SHOW COLUMNS FROM ' . $table . ';';
+        $result = $dbc -> queryObjectArray($sql);
+        
+        // Generate the duplication query with those fields except the key
+        $query = 'INSERT INTO ' . $table . ' (SELECT ';
+        
+        foreach ($result as $row) 
+        {
+            if ($row->Field == $key_name) {
+            $query .= 'NULL, ';
+            } else {
+            $query .= $row->Field . ', ';
+            } // END IF
+        } 
+        
+        $query = substr($query, 0, strlen($query) - 2);
+        $query .= ' FROM ' . $table . ' WHERE ' . $key_name . ' = "' . $key_value . '")';
+
+        $ret = $dbc -> execute($query);
+        if ($ret)
+        { 
+            // Return the new id
+            $new_id = $dbc -> insertId();
+            return $new_id;
+        }
+        else
+            return false;
+/*
+        return $query;
+*/    
+    } 
        
 ?>
\ No newline at end of file