【问题标题】:PHP and Mysql, export from one table and import to another onePHP 和 Mysql,从一张表导出并导入到另一张表
【发布时间】:2014-03-29 18:11:39
【问题描述】:

我正在尝试从一个表中的两个列中导出文本以使用 html 对其进行格式化,然后将其合并到一个新表中的一个列中。

我设法导出所有内容,但无法将 $text_comp 正确插入 post_content。此代码将每行的 0 插入到 post_content 列中。

这是我的代码:

function clean_string($value) {
if(get_magic_quotes_gpc() ) {
        $value = stripslashes($value);
}
return mysql_real_escape_string($value);
}



$result = mysqli_query($con,"SELECT * FROM  table8");
if (!$result) {
    printf("Error: %s\n", mysqli_error($con));
    exit();}




while($row = mysqli_fetch_array($result))
  {



$id_article_c =  $row['id_article_complement'];
$id_article_n =  $row['id_article'];
$text_comp =  "<h3>". $row['titre_article_complement']."</h3><p>".$row['contenu_article_complement'] ."</p>";



$test_insert = mysqli_query($con,'INSERT INTO wordpress_test(post_content, id_article,id_article_complement) VALUES("'.clean_string($text_comp).'", "'.$id_article_n.'","'.$id_article_c.'")');
 if (!$test_insert) {
     printf("Error: %s\n", mysqli_error($con));
    exit();}
}

编辑:我的错,这段代码有效。问题来自我设置数据类型错误的 SQL 表。我不记得确切但它类似于int 而不是TEXT

【问题讨论】:

  • 很抱歉这段代码确实有效,问题出在 MySQL 表的结构上。

标签: php mysql merge insert-into


【解决方案1】:

为什么要使用两个查询? SQL 可以在单个查询中执行此操作:

INSERT INTO wordpress_test(post_content, id_article, id_article_complement)
    SELECT titre_article_complement, id_article, id_article_complement
    FROM  table8;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多