Muzzy
Muzzy

Reputation: 51

how can I add same value in two different tables

i m trying to add custom list of tags using excel file and I have done most of the work. but i am confused that how can I add same value in 2 different tables columns.

i had inserted the values in

INSERT INTO `steve`.`wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, 'tag99', 'tag99', '0');

and

INSERT INTO `steve`.`wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, '21', 'post_tag', '', '0', '0');

now i want to add term_id in both table .. could somebody tell me how can i do that?

AND term_id is database generated value.

Upvotes: 0

Views: 159

Answers (3)

Rick Calder
Rick Calder

Reputation: 18685

INSERT INTO `steve`.`wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, LAST_INSERT_ID, 'post_tag', '', '0', '0');

If I follow correctly your first query inserts into a table and creates a new id for that record, then you want to use that ID in the next query to insert into a different table right? If that's the case the above query should work for your second one using LAST_INSERT_ID for the term_id value.

To update the question with your new code it should, I emphasize should, work with this:

 $sql2 = "INSERT INTO " . $table2 . " (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, LAST_INSERT_ID, 'post_tag', '', '0', '0')";

        $wpdb->query($sql);
        $wpdb->query($sql2);

The way you have it above you are overwriting your first query before you execute it.

Also I don't understand how you're inserting nulls into an auto-increment field, that alone should be throwing an error. Honestly both your queries should be leaving their base ids (the auto increment ones) out of the query entirely like this:

$sql2 = "INSERT INTO " . $table2 . " (term_id, taxonomy, description, parent, count) VALUES (LAST_INSERT_ID, 'post_tag', '', '0', '0')";

Upvotes: 1

Ravi
Ravi

Reputation: 2086

Use mysql_insert_id()

mysql_query("INSERT INTO `steve`.`wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, 'tag99', 'tag99', '0')");

$lastInsertId = mysql_insert_id();

mysql_query("INSERT INTO `steve`.`wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, '" . $lastInsertId . "', 'post_tag', '', '0', '0')");

NOTE: To use mysql_insert_id(), term_id in wp_terms must be set to auto increment.

Upvotes: 1

Teena Thomas
Teena Thomas

Reputation: 5239

you need to use ALTER table and add that column's definition/constraints into the other table, here's the manual

Upvotes: 0

Related Questions