php - 带有子记录的重复 MYSQL 记录

标签 php mysql

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

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

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

关于php - 带有子记录的重复 MYSQL 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30229203/

相关文章:

python - django 无法连接 aws 上的 mysql 服务器

mysql - 适用于 MariaDB 但在 MySQL 上因语法错误而失败的查询

php - 浏览器中的模型 SQL 语法错误是否有更简单的快捷方式替换

java - 如何在 Spring Boot 中扩展类而不更改 MySQL 架构?

php - 如何根据列值组合检索唯一行?

php - 使用jquery加载xml文件,编辑它,然后使用php再次保存到服务器

mysql - #1264 超出范围值修复?

java - Servlet,从html文件获取参数和doGET doPOST

php - 最佳关键字搜索技术

php - 查询时对相关模型的 Yii 限制