我正在使用下面的代码在我的数据库中复制一个事件记录,问题是我还试图复制任何子记录(即事件服务)。我需要它从 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/