Unable to insert data into multiple wordpress tabl

2019-03-04 14:58发布

问题:

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.