0

请帮帮我。我想从联系人表插入到发件箱表。

接触:

CREATE TABLE `contacts` 
 (`id` int(11) NOT NULL auto_increment,
  `name` varchar(25) NOT NULL,
  `phone` varchar(20) NOT NULL,
  `group_name` varchar(20) NOT NULL,
  PRIMARY KEY  (`id`)
 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7;

INSERT INTO `contacts` VALUES (1, 'jala', '+60111', 'friends');
INSERT INTO `contacts` VALUES (2, 'jali', '+60222', 'friends');
INSERT INTO `contacts` VALUES (3, 'jalu', '+60333', 'friends');
INSERT INTO `contacts` VALUES (4, 'kada', '+60444', 'members');
INSERT INTO `contacts` VALUES (5, 'kadi','+60555','members');
INSERT INTO `contacts` VALUES (6, 'kadu', '+60666', 'members');

发件箱:

CREATE TABLE `outbox`
(`id` int(11) NOT NULL,
 `phone_number` varchar(20) NOT NULL,
 `text` varchar(160) NOT NULL,
 PRIMARY KEY  (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8;}

我想插入发件箱表。

插入.php

$sql =mysql_query("SELECT phone 
                   FROM contacts 
                   WHERE group_name = 'member'");
while($data = mysql_fetch_array($sql))
{  $id= $data['id']; $phone= $data['phone'];(phone_number, text) VALUES('$phone','I LOVE YOU') }?>
4

2 回答 2

2
INSERT INTO outbox (id,phone_number,text)
SELECT id,phone,'I LOVE YOU'
FROM contacts
WHERE group_name = 'member'
于 2012-08-17T07:59:12.610 回答
1

您需要INSERT...SELECT在查询中使用该语句。并为此使用PDO 扩展。

例子:

<?php

    $sqlStatement = "INSERT INTO outbox (id, phone_number, text)
                     SELECT id, phone, ?
                     FROM   contacts
                     WHERE  group_name = ?";

    $text = "I LOVE YOU";
    $group_name = "member";

    $stmt = $dbh->prepare($sqlStatement);
    $stmt->bindParam(1, $text);
    $stmt->bindParam(2, $group_name);

    $stmt->execute();

?>

请记住始终清理您的输入。

于 2012-08-17T08:02:54.097 回答