0
$feedback_list = array( 
    array('date1', '87586cb8b79861edcc4e6a12104b87529c53050375904ff180'), 
    array('date2', 'abc1d08c3e42cffa7eaaa84eb4fe04b44c34be497ce7e8ea26') 
);

foreach($feedback_list as $feedback) {
    $result = mysql_query("INSERT INTO 'inactive_users' (token, username) 
       SELECT (token, username) FROM active_users WHERE token = '$feedback[1]'");
    if (!mysql_query($result,$con)) { die('Error: ' . mysql_error()); }
}

.. 回显正确的值,该值也存在于active_users表中,但该值不会复制到第二个表中。错误是:

错误:查询为空

4

3 回答 3

1

您永远不应该将 mysql_query 放在循环中。

INSERT 语句可以聚合成一个大字符串。

与我在开头所说的相反,您应该为您的选择语句使用循环,然后获取结果并连接您的插入查询;在循环之后执行它

一个例子

$feedback_list = array( 
array('date1', '87586cb8b79861edcc4e6a12104b87529c53050375904ff180'), 
array('date2', 'abc1d08c3e42cffa7eaaa84eb4fe04b44c34be497ce7e8ea26') 
);
$insert = null;
$select = "SELECT (token, username) FROM active_users WHERE token = '%s'";
foreach($feedback_list as $feedback) {
    $sql = print_f($select, $feedback[1]);
    $result = mysql_query($sql) or die("SQL:$sql<br />\n". mysql_error());
    $values = mysql_fetch_row($result);
    //$values['token'] => token
    //$values['username'] => username

    $insert .= "INSERT INTO 'inactive_users' (token, username) VALUES ('{$values['token']'}','{$values['username']}')\n";
}
 $theInsert = mysql_query($insert);
于 2012-05-29T15:24:51.453 回答
1

你跑了mysql_query两次。尝试

foreach($feedback_list as $feedback) {
    $result = "INSERT INTO `inactive_users` (token, username) 
       SELECT token, username FROM active_users WHERE token = '{$feedback[1]}'";
    if (!mysql_query($result,$con)) { die('Error: ' . mysql_error()); }
}
于 2012-05-29T15:26:38.280 回答
1

它应该是这样的:

foreach($feedback_list as $feedback) {
    $result = mysql_query("INSERT INTO inactive_users (token, username) SELECT token, username FROM active_users WHERE token = '{$feedback[1]}'");
}
于 2012-05-29T15:07:38.863 回答