可能重复:
无法将数据插入到多个 wordpress 表中
根据上图,我有 2 个表,每个表有 1 个公共列,但问题是我必须在两列行中添加相同的值。虽然wp_terms.term_id
在数据库中有自动增量。但我不明白我如何wp_terms.term_id
在wp_term_taxonomy.term_id
.
我尝试了一个 sql 查询,我在下面给出:
// create a tag
$query = "INSERT INTO $wpdb->terms (name, slug) VALUES (%s, %s)";
$wpdb->query($wpdb->prepare($query, $name, $slug));
// 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'));
但是当我通过 phpmyadmin 检查数据库时运行此脚本后,我注意到wp_terms
表格填写正确但wp_term_taxonomy
>term_id
列是空的。
我的实际代码是:
$file_name = $_FILES['tag_import']['name'];
$file_ext = strtolower(end(explode(".", $file_name)));
$file_size = $_FILES['tag_import']['size'];
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][1], $data->sheets[0]['cells'][$i][2]));
// 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>";
}
}
请指导我该怎么做?