Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用子记录复制MYSQL记录_Php_Mysql - Fatal编程技术网

Php 使用子记录复制MYSQL记录

Php 使用子记录复制MYSQL记录,php,mysql,Php,Mysql,我使用下面的代码复制数据库中的事件记录,问题是我试图复制任何子记录(即事件服务)。我需要它从eventservices表复制所有“事件服务”,并在复制到新复制的id记录期间更新eventid。任何帮助都将不胜感激。谢谢 注意:eventservices表有一个与事件id匹配的eventid字段 $table = 'events'; $id_field = 'id'; $id = $_GET['eventid']; DuplicateMySQLRecord($table, $id_field, $

我使用下面的代码复制数据库中的事件记录,问题是我试图复制任何子记录(即事件服务)。我需要它从eventservices表复制所有“事件服务”,并在复制到新复制的id记录期间更新eventid。任何帮助都将不胜感激。谢谢

注意: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;

    }

请使用下面的代码。我还没有编译这段代码,所以在使用前测试

<?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);
    }
}
?>

好的,我对所有这些进行了一些调整

下面是我的函数,一切都很好,但是,它只得到第一个记录,可能有多个孩子。谢谢你的帮助

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;
}

让我知道您是如何管理子事件的?您是否使用任何父id?是的,子事件有一个列eventid,它存储事件的id首先不要执行insert THEN update(2个mysql查询)。准备新事件而不是插入(1个mysql查询)。效率更高!