带有子记录的MYSQL记录重复

时间:2015-05-14 04:26:15

标签: php mysql

我使用下面的代码在我的数据库中复制一个事件记录,问题是我试图复制任何子记录(即事件服务)。我需要它来复制所有"事件服务"来自eventservices表,以及在复制期间将eventid更新为新复制的id记录。任何帮助,将不胜感激。感谢。

注意:eventservices表有一个与事件id匹配的eventid字段。

$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 the original record into an array
      $result = mysql_query("SELECT * FROM {$table} WHERE {$id_field}={$id}");
      $original_record = mysql_fetch_assoc($result);

      // insert the new record and get the new auto_increment id
      mysql_query("INSERT INTO {$table} (`{$id_field}`) VALUES (NULL)");
      $newid = mysql_insert_id();

      // generate the query to update the new record with the previous values
      $query = "UPDATE {$table} SET ";
      foreach ($original_record as $key => $value) {
        if ($key != $id_field) {
            $query .= '`'.$key.'` = "'.str_replace('"','\"',$value).'", ';
        }
      }
      $query = substr($query,0,strlen($query)-2); # lop off the extra trailing comma
      $query .= " WHERE {$id_field}={$newid}";
      mysql_query($query);

      // return the new id
      return $newid;

    }

2 个答案:

答案 0 :(得分:0)

请使用以下代码。我没有编译此代码,因此请在使用前进行测试。

<?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 the original record into an array
  $result = mysql_query("SELECT * FROM {$table} WHERE {$id_field}={$id}");
  $original_record = mysql_fetch_assoc($result);

  // insert the new record and get the new auto_increment id
  mysql_query("INSERT INTO {$table} (`{$id_field}`) VALUES (NULL)");
  $newid = mysql_insert_id();

  // generate the query to update the new record with the previous values
  $query = "UPDATE {$table} SET ";
  foreach ($original_record as $key => $value) {
    if ($key != $id_field) {
        $query .= '`'.$key.'` = "'.str_replace('"','\"',$value).'", ';
    }
  }
  $query = substr($query,0,strlen($query)-2); # lop off the extra trailing comma
  $query .= " WHERE {$id_field}={$newid}";
  mysql_query($query);

  if($newid) {
    $oldid = $id;
    copychilds($table, 'eventid', $oldid,$newid);
  } 
  // return the new id
  return $newid;

}

function copychilds($table, $id_field, $oldid,$newcopiedid) {
    $result = mysql_query("SELECT * FROM {$table} WHERE id={$oldid}");
    while($original_child_record = mysql_fetch_assoc($result){
        // insert the new record and get the new auto_increment id
        mysql_query("INSERT INTO {$table} (`id`) VALUES (NULL)");
        $newid = mysql_insert_id();

        // generate the query to update the new record with the previous values
        $query = "UPDATE {$table} SET ";
        foreach ($original_record as $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 the extra trailing comma
        $query .= " WHERE id={$newid}";
        mysql_query($query);
    }
}
?>

答案 1 :(得分:0)

好的,我已经完成了所有这些调整。

下面是我的功能,一切都很好,但是,它只获得第一个记录,可能有多个孩子。任何帮助赞赏。

function copychilds1($table, $id_field, $oldid,$newcopiedid, $updatedid) {
    include_once '../inc/db_connect.php';

  // load the original record into an array
  $result = mysql_query("SELECT * FROM {$table} WHERE {$id_field}={$oldid}");
  $original_record = mysql_fetch_assoc($result);

  // insert the new record and get the new auto_increment id
  mysql_query("INSERT INTO {$table} (`{$id_field}`) VALUES (NULL)");
  $newid = mysql_insert_id();
  // generate the query to update the new record with the previous values
  $query = "UPDATE {$table} SET ";
  foreach ($original_record as $key => $value) {
    if ($key != 'id') {
        $query .= '`'.$key.'` = "'.str_replace('"','\"',$value).'", ';
    }
  }
  $query = substr($query,0,strlen($query)-2); # lop off the extra trailing comma
  $query .= " WHERE id={$newid}";
  mysql_query($query);
  $finalquery = "UPDATE eventservices SET eventid = {$updatedid} WHERE id = {$newid}";
  mysql_query($finalquery);

  // return the new id
  return $newid;
}