跨多个相关表重复行数据。
在php中,使用第一个 mysqli_multi_query 后,我似乎无法从 mysqli_insert_id 获取id结果。
我使用phpmyadmin成功查询了以下内容(手动将unit_id替换为1,将$ unit_id1替换为61(下一个对应)):
CREATE TEMPORARY TABLE tmp
SELECT `unit_id`,
`title`,
`status_id`,
`category_id`,
`tags`,
`access_id`
FROM unit_genData
WHERE `unit_id` = 1;# 1 row affected.
ALTER TABLE tmp
DROP COLUMN `unit_id`;# 1 row affected.
UPDATE tmp
SET `title` = 'DUPLICATE';# 1 row affected.
INSERT INTO unit_genData
SELECT 0,tmp.*
FROM tmp;# 1 row affected.
DROP TABLE tmp;# MySQL returned an empty result set (i.e. zero rows).
CREATE TEMPORARY TABLE tmp
SELECT `ad_id`,
`unit_id`,
`ad_title`,
`ad_image`,
`ad_img_caption`,
`ad_brief_desc`,
`ad_btn_text`
FROM unit_promoContent
WHERE `unit_id`=1;# 1 row affected.
ALTER TABLE tmp
DROP COLUMN `ad_id`;# 1 row affected.
UPDATE tmp
SET `unit_id` = 61;# 1 row affected.
INSERT INTO unit_promoContent
SELECT 0,tmp.*
FROM tmp;# 1 row affected.
DROP TABLE tmp;# MySQL returned an empty result set (i.e. zero rows).
注意:第一个multi_query成功复制了第一个表...第二个multi_query依赖于mysqli_insert_id结果。
$sql1 = "CREATE TEMPORARY TABLE tmp
SELECT `unit_id`,
`title`,
`status_id`,
`category_id`,
`tags`,
`access_id`
FROM ".ID_TABLE."
WHERE `unit_id` = " . $id . ";
ALTER TABLE tmp
DROP COLUMN `unit_id`;
UPDATE tmp
SET `title` = 'DUPLICATE';
INSERT INTO ".ID_TABLE."
SELECT 0,tmp.*
FROM tmp;
DROP TABLE tmp;
";
$result = mysqli_multi_query($dbc,$sql1)
or die(mysqli_error($sql1));
$unit_id1 = mysqli_insert_id($dbc); // Store new unit_id as var // Tab 2 :: Promo Content
$sql2 = "CREATE TEMPORARY TABLE tmp
SELECT `ad_id`,
`unit_id`,
`ad_title`,
`ad_image`,
`ad_img_caption`,
`ad_brief_desc`,
`ad_btn_text`
FROM unit_promoContent
WHERE `unit_id`=" . $id . ";
ALTER TABLE tmp
DROP COLUMN `ad_id`;
UPDATE tmp
SET `unit_id` = ". $unit_id1 .";
INSERT INTO unit_promoContent
SELECT 0,tmp.*
FROM tmp;
DROP TABLE tmp;
";
$result = mysqli_multi_query($dbc,$sql2)
or die(mysqli_error($sql2));
答案 0 :(得分:1)
最后,在经过大量测试后,我发现成功获取新行的unit_id的唯一方法是将第一个mysqli_multi_query
分隔为单个mysqli_query
。
即使在这样做之后,我仍然发现我得到了一个解析错误,所以我将mysqli_insert_id
直接移到INSERT
查询的下方。
现在我已经能够获得新的unit_id,我能够成功运行mysqli_multi_query
以获取下一个重复的表。 然而,我遇到了同样的问题,包括要复制的剩余表格,所以我终于发现我必须将所有mysqli_multi_query
分成单独的mysqli_query
。
请参阅下面的工作解决方案:
注意:
ID_TABLE
在包含的config.php文件中定义(此处未显示)。这是一张标题为unit_genData
的表格
*$id*
是一个var,表示要复制的所选/已检查行的初始unit_id
$sql1 = "CREATE TEMPORARY TABLE tmp
SELECT `unit_id`,
`title`,
`status_id`,
`category_id`,
`tags`,
`access_id`
FROM ".ID_TABLE."
WHERE `unit_id` = " . $id . "";
$result = mysqli_query($dbc,$sql1) or die(mysqli_error($dbc));
$sql2 = "ALTER TABLE tmp
DROP COLUMN `unit_id`";
$result = mysqli_query($dbc,$sql2) or die(mysqli_error($dbc));
$sql3 = "UPDATE tmp
SET `title` = 'DUPLICATE'";
$result = mysqli_query($dbc,$sql3) or die(mysqli_error($dbc));
$sql4 = "INSERT INTO ".ID_TABLE."
SELECT 0,tmp.*
FROM tmp";
$result = mysqli_query($dbc,$sql4) or die(mysqli_error($dbc));
$unit_id1 = mysqli_insert_id($dbc); //$dbc->insert_id; // Store new unit_id as var
$sql5 = "DROP TABLE tmp;";
$result = mysqli_query($dbc,$sql5) or die(mysqli_error($dbc));
$data = "Insert_id for TABLE 1: ".$unit_id1.". ";
// Duplicate Table for Tab 2
// Promo Content
$sql6 = "CREATE TEMPORARY TABLE tmp
SELECT `ad_id`,
`unit_id`,
`ad_title`,
`ad_image`,
`ad_img_caption`,
`ad_brief_desc`,
`ad_btn_text`
FROM unit_promoContent
WHERE `unit_id`=" . $id . ";
$result = mysqli_query($dbc,$sql6) or die(mysqli_error($dbc));
$sql7 = "ALTER TABLE tmp
DROP COLUMN `ad_id`";
$result = mysqli_query($dbc,$sql7) or die(mysqli_error($dbc));
$sql8 = "UPDATE tmp
SET `unit_id` = ". $unit_id1 .";
$result = mysqli_query($dbc,$sql8) or die(mysqli_error($dbc));
$sql9 = "INSERT INTO unit_promoContent
SELECT 0,tmp.*
FROM tmp;";
$result = mysqli_query($dbc,$sql9) or die(mysqli_error($dbc));
$sql10 = "DROP TABLE tmp;";
$result = mysqli_query($dbc,$sql10) or die(mysqli_error($dbc));
# ======================================================= #
#...and so on...for the rest of the tables to duplicate...#
# ======================================================= #
mysqli_close($dbc);