Jump to content

How do I take data from a select query and insert into another table?


kamlooper

Recommended Posts

I have tried several attempts and can get data to select and using echo display it, however, I need to take this data and insert it into the database in a separate table.  I have the following which does hafl the job, can I get some pointers on the rest.  I have looked everywhere and not found a solution, at all.

 

 

// Selects the data I need

<?php

mysql_connect("PRIVATE INFO","PRIVATE INFO","PRIVATE INFO") or

    die("Could not connect: " . mysql_error());

 

mysql_select_db("wpdb");

 

$result = mysql_query("SELECT ID FROM wp_posts

 

WHERE post_title LIKE '%future%'

AND post_status = 'publish'

OR post_title LIKE '%option%'

AND post_content LIKE '%fundamental%'

AND post_status = 'publish'

 

ORDER BY post_date DESC");

 

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

{ echo $row['ID'];

echo "<br />";};

 

?>

 

 

 

Thought maybe something like the following would work, but am at a loss:

INSERT INTO wp_term_relationships (object_id, term_taxonomy_id, term_order)

      SELECT ID FROM wp_posts

 

            WHERE post_title LIKE '%future%'

            AND post_status = 'publish'

            OR post_title LIKE '%option%'

            AND post_content LIKE '%fundamental%'

            AND post_status = 'publish'

 

            ORDER BY post_date DESC

 

            while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

 

VALUES

($row['ID'], '25', '0')

 

 

 

I have looked at your suggestion and have a few questions.

 

I am only pulling the ID information from one table to post into a second, leaving the other two columns to be auto populated with information I will provide.

 

Example (value1, value2, value3)  =  (value1 carried out multiple times from a looking array, value2 a number i set, lavue 3 again a number i set)

 

(row[iD], 25, 0)

 

I dont know if this makes any sense, I have been looking at it so long i am cross eyed.

 

basically i want to take ID data (multiple results) from a query of one table and insert it with other hard coded data into a separate table? :shrug:

 

I used the following and it worked!!!  THANK YOU.

 

 

$sql="INSERT INTO wp_term_relationships (field1, field2, field3)

 

SELECT ID, hardcoded number1, hardcoded number2 FROM wp_posts

      WHERE post_title

      LIKE '%future%'

      AND post_status = 'publish'

      OR post_title LIKE '%option%'

      AND post_content LIKE '%fundamental%'

      AND post_status = 'publish'

    ";

 

 

    if (!mysql_query($sql,$con))

      {

      die('Unfortunately the ID chosen is already in use please click the back button and select a different ID.' );

      }

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.