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')

 

 

 

Link to comment
Share on other sites

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:

 

Link to comment
Share on other sites

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.' );

      }

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.