将mysql行移动到同一数据库中的另一个表 [英] Move mysql row to another table in the same database

查看:72
本文介绍了将mysql行移动到同一数据库中的另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,在这方面,我是一个 n00b.我感谢这个网站给我的所有帮助.所以我再次陷入困境.我想将一个表(约会)中的条目复制到另一个表(已删除).我在删除条目方面得到了一些帮助,该代码将在主代码中,所以它是:

I am a n00b when it comes to this stuff, apparently. I appreciate all the help this site has given me. So I am, once again, stuck on something. I want to copy over an entry from one table (appointments) to another table (deleted). I've gotten some help on deleting entries and that code will be in the main code, so here it is:

$stmt = mysqli_prepare($transport, "DELETE FROM appointments WHERE id = ?");

mysqli_stmt_bind_param($stmt, "i", $id);
foreach ($_POST['delete'] as $id) 
    {
        mysqli_stmt_execute($stmt);
    }

删除代码就像一个魅力.我也有更新字段的代码:

The delete code works like a charm. I also have code to update the fields:

$size = count($_POST['room_n']);
$i=0;
while ($i < $size)
{
    $room_n = mysqli_real_escape_string($transport, $_POST['room_n'][$i]);
    $id = $_POST['id'][$i];
    $sql = "UPDATE appointments SET room = '$room_n' WHERE id = $id";
    mysqli_query($transport, $sql) or die('Error: ' . mysqli_error($transport));

    ++$i;

}

这也 100% 有效.我尝试使用相同类型的 while 循环将已删除的项目插入到删除表中,但它只会给我错误.我还尝试将插入代码放在 mysqli_stmt_execute 前面,但我发现它没有发布.所以我不确定我能做什么.再次感谢您的帮助!

Which also works 100%. I've tried to use the same type of while loop for the insertion of the deleted items to the delete table and it just gives me errors. I've also tried putting the insert code in front of mysqli_stmt_execute and I get it just doesn't post. So I am not sure what I can do. Thank you for the help again!

推荐答案

您可以使用功能 INSERT ... SELECT

You could use feature INSERT ... SELECT

INSERT INTO `deleted` SELECT * FROM appointments WHERE id = $id

附言不要忘记使用事务,因为您正在执行具有两个查询的操作.

P.S. Do not forget to use transaction because you are performing an operation with two queries.

P.P.S.最好添加状态字段,例如 id_deleted

P.P.S. Better to add status field, like id_deleted

P.P.P.S.不要使用mysql_函数

P.P.P.S. Do not use mysql_ functions

这篇关于将mysql行移动到同一数据库中的另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆