简体   繁体   English

Mysql / PHP更新表+插入另一个表

[英]Mysql/PHP UPDATE the table + INSERT INTO an other table

Can I copy something to an other table and also update this in the same table? 我可以复制内容到另一个表中,也可以在同一张表中更新它吗?

Like: 喜欢:

INSERT INTO 'table_new' (name) values ("thomas") 

At the same time: 与此同时:

UPDATE 'table_old' set ChangesWereMadeAt = (the date, where the changes were made) 

Can I put something in other table, while it stays also in the old table and just updates one column ? 我可以在其他表中放一些东西,而同时又保留在旧表中并只更新一列吗?

I work with PHP/MySql 我使用PHP / MySql

use LAST_INSERT_ID(); 使用LAST_INSERT_ID();

INSERT INTO 'table_new' (name) values ("thomas");
$last_id_inserted = LAST_INSERT_ID(); 

UPDATE 'table_old' SET blah='$blah' WHERE id='$last_id_inserted';

Begin by inserting your new line into your table. 首先将新行插入表中。

Then delete the line from the first table. 然后从第一个表中删除该行。

And finally copy the line you inserted with this: 最后复制与此插入的行:

INSERT INTO first_table
SELECT *
FROM dues
WHERE id = 5;

Like: INSERT INTO 'table_new' (name) values ("thomas") 像:INSERT INTO'table_new'(名称)值(“托马斯”)

At the same time: 与此同时:

UPDATE 'table_old' set ChangesWereMadeAt = (the date, where the changes were made) UPDATE'table_old'设置ChangesWereMadeAt =(进行更改的日期)

Based on this, I'd say the solution would be to make two requests. 基于此,我想说解决方案是提出两个请求。

This means... Can I put something in an other table while it stays also in the old table and just updates one column ? 这意味着...我可以将某些东西放到另一个表中,同时又将其保留在旧表中并只更新一个列吗?

But based on this, I could give a sort of solution. 但是基于此,我可以提出一种解决方案。

$reqSelect= 'select * from YourTable 
where [insert the condition that would make you select only the data you want to copy]';
$data = mysqli_query($connection, $reqSelect);

$reqInsert='insert into YourDestinationTable(row1, row2, etc)
values('.$data[row1].', '.$data[row2].', '.$data[etc]);
mysqli_query($connection, $reqInsert);

$reqUpdate='update YourTable where [conditions here]
set TheRowYouWantToModify = '.data[TheDataYouWantInYourRow];
mysqli_query($connection, $reqUpdate);

This sounds like it would be easier to solved my a MySql trigger. 听起来这似乎更容易解决我的MySql触发器。 There is a basic example here: Creating Triggers to add the data into Audit Table 这里有一个基本示例: 创建触发器以将数据添加到审核表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM