dongmi8980 2011-07-26 02:07
浏览 46
已采纳

将数组从一个表插入另一个表

I'm afraid this is going to sound redundant. I have done my diligence, and substituted at least three ideas I've found so far, and nothing works.

The goal is to merge unique fields from two tables, but right now I can't even get identical fields from one table into another that was created as an identical table. Here is the code so far, with my comments:

$result = mysql_query("SELECT * from $db.$tbl LIMIT 50");

if($result) {
while($maindata = mysql_fetch_assoc($result)) {

$newdata = implode ("," , $maindata);

$newdata = mysql_escape_string($newdata);

$pop = "INSERT INTO $db.address_new (id,entitynum,address,city,state,zip,country,remarks)
SELECT FROM $db.address";        

//This is the original code replaced by the above
//$pop = "INSERT INTO $db.address_new (id,entitynum,address,city,state,zip,country,remarks)
//  VALUES ($newdata)"; 

mysql_query($pop);

//print_r($pop);
   //Seems to give the correct output in the browser, but the table address_new remains empty. `

Thank you in advance. I really appreciate your help.

  • 写回答

1条回答 默认 最新

  • doulu5109 2011-07-26 02:16
    关注

    To directly insert from another table (note: if ID is auto_increment you may or may not want to insert it this way):

    INSERT INTO db.address_new (id,entitynum,address,city,state,zip,country,remarks)
       SELECT (id,entitynum,address,city,state,zip,country,remarks) 
          FROM db.address LIMIT 50
    

    (do not put this in a loop)

    If you're looking for unique values, you can do that a couple of ways. Personally, I would have a unique key on one (or a set of) the values and then just do INSERT IGNORE:

    INSERT IGNORE INTO db.address_new 
      (id,entitynum,address,city,state,zip,country,remarks)
       SELECT (id,entitynum,address,city,state,zip,country,remarks) 
          FROM db.address LIMIT 50
    

    As a side note:

    // use mysql_real_escape_string 
    mysql_escape_string($newdata); 
    
    // since you're passing this through PHP, you need to make sure to quote 
    // all of your values. This probably means you'll need to loop and replace this
    // $newdata = implode ("," , $maindata);
    // with something like:
    
    $newdata = array();
    foreach( $maindata as $column )
    {
        $newdata[] = "'" . mysql_real_escape_string( $column ) . "'";
    }
    $newdata = implode(',', $newdata);
    
    
    // you're missing the columns in your select clause. 
    $pop = "INSERT INTO $db.address_new ".
           "(id,entitynum,address,city,state,zip,country,remarks) ".
           // You need to select *something*
           "SELECT FROM $db.address"; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?