简体   繁体   English

MySQL插入循环仅插入一行

[英]MySQL insert loop only inserts one row

I'm doing a one-time import of data (so overhead is not an issue) from one old table into a Wordpress comments table. 我正在将数据从一个旧表一次性导入到Wordpress注释表中(因此开销不是问题)。 I loop through the old table, use php to tweak some of the data for the new table, then perform each insert. 我遍历旧表,使用php调整新表的一些数据,然后执行每次插入。 It'll only insert the first row though. 不过,它只会插入第一行。 If I run the code again, it'll insert the same first row over, so I don't think there's a primary key issue. 如果我再次运行代码,它将在同一行插入同一行,因此我认为没有主键问题。

require('wp-config.php');

$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME); 

$result = mysqli_query($con,"SELECT *
    FROM user_import u, wp_posts p, wp_postmeta m
    WHERE p.ID = m.post_id
    AND m.meta_key = 'fanfic_id'
    AND m.meta_value = u.fanfic_id");

while($row = mysqli_fetch_array($result))
  {
    $nick=$row['user_nickname'];
    $ip=$row['user_ip_address'];
    $date=strToTime($row['user_fanfic_date']);
    $date2=strftime('%Y-%m-%d 12:00:00',$date);
    $gmt=strftime('%Y-%m-%d 12:00:00', $date);
    $datemine = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date)));

    $fanfic_id=$row['fanfic_id'];

    $getPostID="SELECT post_id FROM wp_postmeta WHERE meta_key='fanfic_id' and meta_value=$fanfic_id";

    $result2 = mysqli_query($con,$getPostID );

    while($row2 = mysqli_fetch_array($result2)){
      $post_id=$row2['post_id'];

    }

    $review=$row['user_fanfic_review'];
    $sql="INSERT INTO wp_comments(comment_approved, user_id, comment_post_id, comment_author, comment_author_ip, comment_date, comment_date_gmt, comment_content)
      VALUES(1, 0, $post_id,'$nick','$ip', '$date2', '$gmt', '$review') ";

    $result = mysqli_query($con,$sql);

  }

mysqli_close($con);

I believe because of this line 我相信因为这条线

$sql="INSERT INTO wp_comments(comment_approved, user_id, comment_post_id, comment_author, comment_author_ip, comment_date, comment_date_gmt, comment_content)
  VALUES(1, 0, $post_id,'$nick','$ip', '$date2', '$gmt', '$review') ";
$result = mysqli_query($con,$sql); // <--- this line.

Since you reassign the $result of the SELECT to the result of the INSERT. 由于您将SELECT的$result重新分配给INSERT的结果。 Can you change that line to only this? 您可以将该行更改为仅此行吗?

 mysqli_query($con,$sql);

The problem exists at this point: 此时存在问题:

$getPostID="SELECT post_id FROM wp_postmeta WHERE meta_key='fanfic_id' and meta_value=$fanfic_id";

$result2 = mysqli_query($con,$getPostID );

while($row2 = mysqli_fetch_array($result2)){
 $post_id=$row2['post_id'];
}

What you are doing is always getting the same post_id inside the loop. 您正在执行的操作总是在循环内获得相同的post_id That means everytime when the first while loop while($row = mysqli_fetch_array($result)) runs the second while fetches the same data and continue insert operation. 这意味着每当第一个while循环while($row = mysqli_fetch_array($result))运行第二个while时,while都会获取相同的数据并继续进行插入操作。

PS Consider using $wpdb object for wordpress database operation http://codex.wordpress.org/Class_Reference/wpdb . PS考虑将$wpdb对象用于wordpress数据库操作http://codex.wordpress.org/Class_Reference/wpdb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM