php - Duplicate MYSQL Record with child records -


i using code below duplicate event record in database, problem trying duplicate child records (i.e. event services). need copy "event services" eventservices table update eventid during copy newly copied id record. appreciated. thanks.

note: eventservices table has eventid field matches id of event.

$table = 'events'; $id_field = 'id'; $id = $_get['eventid']; duplicatemysqlrecord($table, $id_field, $id);    function duplicatemysqlrecord($table, $id_field, $id) {          include_once 'db_connect.php';       // load original record array       $result = mysql_query("select * {$table} {$id_field}={$id}");       $original_record = mysql_fetch_assoc($result);        // insert new record , new auto_increment id       mysql_query("insert {$table} (`{$id_field}`) values (null)");       $newid = mysql_insert_id();        // generate query update new record previous values       $query = "update {$table} set ";       foreach ($original_record $key => $value) {         if ($key != $id_field) {             $query .= '`'.$key.'` = "'.str_replace('"','\"',$value).'", ';         }       }       $query = substr($query,0,strlen($query)-2); # lop off trailing comma       $query .= " {$id_field}={$newid}";       mysql_query($query);        // return new id       return $newid;      } 

please use below code.i have not compile code test before use.

<?php include_once 'db_connect.php'; $table = 'events'; $id_field = 'id'; $id = $_get['eventid']; duplicatemysqlrecord($table, $id_field, $id); function duplicatemysqlrecord($table, $id_field, $id) {    // load original record array   $result = mysql_query("select * {$table} {$id_field}={$id}");   $original_record = mysql_fetch_assoc($result);    // insert new record , new auto_increment id   mysql_query("insert {$table} (`{$id_field}`) values (null)");   $newid = mysql_insert_id();    // generate query update new record previous values   $query = "update {$table} set ";   foreach ($original_record $key => $value) {     if ($key != $id_field) {         $query .= '`'.$key.'` = "'.str_replace('"','\"',$value).'", ';     }   }   $query = substr($query,0,strlen($query)-2); # lop off trailing comma   $query .= " {$id_field}={$newid}";   mysql_query($query);    if($newid) {     $oldid = $id;     copychilds($table, 'eventid', $oldid,$newid);   }    // return new id   return $newid;  }  function copychilds($table, $id_field, $oldid,$newcopiedid) {     $result = mysql_query("select * {$table} id={$oldid}");     while($original_child_record = mysql_fetch_assoc($result){         // insert new record , new auto_increment id         mysql_query("insert {$table} (`id`) values (null)");         $newid = mysql_insert_id();          // generate query update new record previous values         $query = "update {$table} set ";         foreach ($original_record $key => $value) {             if ($key != 'id') {                 $query .= '`'.$key.'` = "'.str_replace('"','\"',$value).'", ';             }         }         $query .= '`'.$id_field.'` = "'.str_replace('"','\"',$newcopiedid).'", ';         $query = substr($query,0,strlen($query)-2); # lop off trailing comma         $query .= " id={$newid}";         mysql_query($query);     } } ?> 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -