将数组结果重命名和插入到不同的表中

时间:2011-09-15 20:19:04

标签: php mysql

//首先,我分配一个$ variable($ emailzipmatch)来查询一个名为(repzipcodes)的数据库表,并根据客户的邮政编码匹配拉出并显示1到3条记录(RepZipCode = $ CustomerZipMatch)与1到3个其他人(GROUP BY RepId HAVING COUNT(1)< = 3“),希望该客户的信息来自该特定邮政编码。

//代码工作原理

$emailzipmatch = mysql_query("SELECT * FROM repzipcodes WHERE RepZipCode = $CustomerZipMatch GROUP BY RepId HAVING COUNT(1) <= 3") or die(mysql_error());

$recipients = array();
while($row = mysql_fetch_array($emailzipmatch)) 
{ 
$recipients[] = $row['RepEmail'];

echo "Agent's Email Address: ";

echo 'font color="#FF7600"',$row['RepEmail'], '/font';

echo '<br />'; 

echo "Rep's ID: "; 
echo '<br />';
echo 'font color="#FF7600"',$row['RepId'], '/font';
echo '<br />';
echo 'hr align="left" width="50%" size="2" /';
}

//我的问题

//对于上面这个过程的NEXT步骤,我会使用$ row ['RepEmail']和$ row ['RepId'],它可以有1到3个结果,并为1到3个结果分配一个新的$变量所以它可以插入到不同的db表中,这样我就可以从页面顶部跟踪查询结果($ emailzipmatch =):ie ..

<New Variable>   <Listed from above>
$SentRepId 0   = RepId (results from above echo area)
$SentRepId 1   = RepId (results from above echo area)
$SentRepId 2   = RepId (results from above echo area)

//下面我想将上述结果插入新数据库

$?Variable??? = mysql_query("INSERT INTO sentemail 
(SentRepId0, SentRepId1, SentRepId2,SentDateTime
) VALUES (
'$_SESSION[RepId]', // ?????
'$_SESSION[RepId]', // ?????
'$_SESSION[RepId]', // ?????
 NOW()
  )") or die(mysql_error());

//提前感谢你们给我的任何帮助。如果我的编码或请求不清楚,或者由于我缺乏PHP和MySQL经验而导致我感到困惑,请回答任何问题。

2 个答案:

答案 0 :(得分:0)

只要您有用户访问权限,就可以执行以下操作:

$?Variable??? = mysql_query("INSERT INTO db1.sentemail (SentRepId0, SentRepId1,SentRepId2,SentDateTime) VALUES (
'$_SESSION[RepId]', // ?????
'$_SESSION[RepId]', // ?????
'$_SESSION[RepId]', NOW())") or die(mysql_error());

答案 1 :(得分:0)

我想我知道你在这里要做什么。在我需要保留记录一段时间之前,我做了类似的事情所以我在输出中给每个搜索结果一个id,并将它们放在一个表的单独行中以及一个时间戳,以便以后可以清理它或者如果你想按时间搜索(mysql也有自己的时间戳,但如果你还没有,那么这是另一回事)

因此,要制作一个“日志表”,您可能希望设计一些类似于以下内容的东西:

<?php

$search_id = md5(uniqid()).rand(100,999); //// MAKE AN ID FOR THE SEARCH;
$timestamp = time(); // IN CASE YOU WANT TO CLEAN UP AT A LATER DATE, MUST BE AN INTEGER IN MYSQL (so you can use a less than $timestamp in the delete query)

$emailzipmatch = mysql_query("SELECT * FROM repzipcodes WHERE RepZipCode = $CustomerZipMatch GROUP BY RepId HAVING COUNT(1) <= 3") or die(mysql_error()); // if you are just wanting to limit the results look at LIMIT in a mysql tutorial somewhere. 

$recipients = array();
while($row = mysql_fetch_array($emailzipmatch)) 
{ 
//// THE REST OF YOUR CODE HERE (and at the end of the while loop insert in to a table, in your case 'sentemail')

mysql_query("INSERT INTO sentemail (search_id, rep_email, rep_id, zip_code, timestamp) VALUES ('$search_id', '{$row['RepEmail']}', '{$row['RepId']}', $CustomerZipMatch, $timestamp);") or die(mysql_error()); // or what ever details you are after
}

?>

这应该为您提供一个可以按不同标准搜索的参考表。不是最完美的方式,但它将完成工作。

希望能给你一个不同的视角:)

关于“由于我缺乏PHP经验”:当你发展自己的技能时,我肯定会花些时间来研究面向对象的PHP。对于更多服务器繁重或复杂的事情,值得了解。我现在就这样对你说,因为我希望有人早点告诉我。

祝你好运。