Unable to insert data into multiple wordpress tables

只谈情不闲聊 提交于 2019-12-02 23:17:26

问题


I am creating WordPress tags import/export plugin. but I am having an issue.

As we know Tags are actually occupying wp_terms in the database and as well as wp_term_taxonomy.

So I come up with a plan but its not working.

Please check the following codes and let me know how can I fix the issue. Because I am importing name and slug from excel file and trying to save in wp_terms table as well as trying to save term_id into wp_term_taxonomy.

if (( $file_ext == "xls" ) && ( $file_size < 500000 )) 
{       
    $data = new Spreadsheet_Excel_Reader();
    $data->setOutputEncoding('CP1251');
    $data->read($_FILES['tag_import']['tmp_name']);

    for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++)
    {
        for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++)
        {
            // add the new category 
            $query = "INSERT INTO $wpdb->terms (name, slug) VALUES (%s, %s)";
            $wpdb->query($wpdb->prepare($query, $data->sheets[0]['cells'][$i][0], $data->sheets[0]['cells'][$i][1])); 

            // create the relationship 
            $query = "INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy) VALUES (%d, %s)";  
            $wpdb->query($wpdb->prepare($query, LAST_INSERT_ID, 'post_tag'));           
        }
    }
}
else  
{
    echo "<div class='error'><p>Invalid file or file size too big.</p></div>";
}

Please check and let me know.

来源:https://stackoverflow.com/questions/13169276/unable-to-insert-data-into-multiple-wordpress-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!