简体   繁体   English

从一个表插入数据到另一个PHP SQL

[英]insert data from one table to another php sql

I'm making a private messages application and I am trying to create a 'deleted messages' folder so the messages that the user wants to delete from their inbox or sent folder go to 'deleted'. 我正在制作一个私人消息应用程序,并试图创建一个“已删除消息”文件夹,以便用户要从其收件箱或已发送文件夹中删除的消息转到“已删除”。 I managed to write the code to delete them completely however that is not what i want. 我设法编写代码以完全删除它们,但是那不是我想要的。 I created another table in my database, named 'deleted' so I'm trying to delete them from the inbox and then insert them there. 我在数据库中创建了另一个名为“已删除”的表,因此我试图从收件箱中删除它们,然后将其插入到其中。

This is what I have until now: 这是我到目前为止所拥有的:

if ( isset($_GET['delete'])) {
    $multiple = $_GET['multiple'];

$i=0;
$q = "insert into deleted  (select * from email where to_user = '$user')";
foreach($multiple as $msg_id) 
{ 
        $i++;

    if ($i==1) {
        $q .= " WHERE id = ". mysql_real_escape_string($msg_id)."";
    } else {
        $q .= "OR  id = ". mysql_real_escape_string($msg_id)."";
    }
}

mysql_query($q) or die (mysql_error());
header("location: " .$_SERVER['PHP_SELF']);
exit();
}

It is giving me this error: 它给了我这个错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id = 14' at line 1 检查与您的MySQL服务器版本对应的手册以获取正确的语法,以在第1行的“ WHERE id = 14”附近使用

The output of this script would be: 该脚本的输出为:

insert into deleted  (select * from email where to_user = '$user') WHERE id = 14

You can't have a WHERE on the insert clause. 您不能在insert子句上使用WHERE。 What you are probably looking for is: 您可能正在寻找的是:

insert into deleted  (select * from email where to_user = '$user' AND id = 14)

Which you can account for by adjusting the position of the closing bracket, and adjusting the WHERE clause to be listed as AND instead. 您可以通过调整右括号的位置并调整WHERE子句作为AND来解决。

On further reflection, this ALSO will not accomplish what you want. 经过进一步的思考,此ALSO也无法完成您想要的。 You are attempting to join Multiple ID's together, but the way AND and OR in SQL work, this will not accomplish what you are attempting. 您试图将多个ID连接在一起,但是SQL中AND和OR的工作方式将无法完成您的尝试。 The OR statement would need to be wrapped in brackets to ensure it got caught with the AND statement, like so: OR语句需要包装在方括号中,以确保它被AND语句捕获,如下所示:

INSERT INTO deleted  (SELECT * FROM email WHERE to_user = '$user' AND (id = 14 OR id = 15))

Your code should read like this: 您的代码应如下所示:

$q = "insert into deleted  (select * from email where to_user = '$user' ";
foreach($multiple as $msg_id) 
{ 
    $i++;

    if ($i==1) {
        $q .= " AND (id = ". mysql_real_escape_string($msg_id)."";
    } else {
        $q .= " OR  id = ". mysql_real_escape_string($msg_id)."";
    }
}
$q .= "))"; // Closing out both brackets

why are deleting from this school syntax. 为什么要从此学校语法中删除。 Use Triggers 使用Triggers

something like this : 像这样的东西:

CREATE TRIGGER `my_insert_table2_trigger`     
  AFTER DELETE ON `table1`     
  FOR EACH ROW     
BEGIN
  INSERT INTO `table2` VALUES (OLD.id, OLD.Column_with_value)
END

Just a thought - you could also add a column to your table - 'DELETED_FLAG' perhaps? 只是想一想-您也可以在表格中添加一列-也许是“ DELETED_FLAG”? - that you can set to "Y" when you want it to appear as 'deleted.' -当您希望它显示为“已删除”时可以将其设置为“ Y”。

It'll save some complexity in the long run. 从长远来看,它将节省一些复杂性。

Problem is you have two WHERE clause mentioned in your query as pointed below. 问题是您在查询中提到了两个WHERE子句,如下所示。 Also, remove those () from select part 另外,从select part删除那些()

$q = "insert into deleted  
(select * from email where to_user = '$user')"; <-- Here

foreach($multiple as $msg_id) 
{ 
     if ($i==1) {
        $q .= " WHERE id = ". mysql_real_escape_string($msg_id).""; <-- Here

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

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