0

嗨,我正在编写简单的时事通讯脚本,我在那里有两个小组,每个小组有两个不同的表格。

我想将订阅者从一个组移动到另一个组,我输入了这个查询并且它有效:

$sql = "INSERT INTO newsletter_subscribers2 SELECT * FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";

然而,在移动了一些以前的订阅者之后,我得到了重复输入键的错误,因为我假设查询是用 ID 移动的,所以我只想要移动、姓名、姓氏和电子邮件。

$sql = "INSERT INTO newsletter_subscribers2 VALUES (firstname, lastname, email) SELECT newsletter_subscribers.firstname, newsletter_subscribers.lastname, newsletter_subscribers.email FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";

但是,这部分代码似乎不起作用,因为我遇到了语法错误,任何人都可以帮助我解决这个问题,我会很高兴。

谢谢您的回复

4

3 回答 3

0

删除值

$sql = "INSERT INTO newsletter_subscribers2 (firstname, lastname, email) 
        SELECT newsletter_subscribers.firstname, newsletter_subscribers.lastname, 
               newsletter_subscribers.email 
        FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";

编辑:并始终使用 mysql_real_escape_string 来防止 SQL 注入:

$sql = "... WHERE id='".mysql_real_escape_string($_GET['nid'])."'";
于 2013-10-29T10:50:21.807 回答
0

你必须删除values

  $sql = "INSERT INTO newsletter_subscribers2(firstname, lastname, email) 
   SELECT newsletter_subscribers.firstname, newsletter_subscribers.lastname, 
   newsletter_subscribers.email 
   FROM newsletter_subscribers WHERE id='".$_GET['nid']."'";
于 2013-10-29T10:55:09.243 回答
0

试试这个:

insert into newsletter_subscribers2 (col1, col2, col3, etc) select col1, col2, etc from newsletter_subscribers2  where 1 = 1
于 2013-10-29T11:02:32.943 回答