We help IT Professionals succeed at work.

php assistance needed

CalmSoul
CalmSoul asked
on
I have 3 tables

TABLE 1 COLUMNS:

`test1`(`id`, `title`, `URL`, `img`, `tagsv`

Open in new window


TABLE 2 COLUMNS:

qa_posts (type, categoryid, userid, created, title, content, tags)

Open in new window


TABLE 3 COLUMNS:

qa_postmetas (postid, title, content) 

Open in new window


- I am looking for php script which will get data from table 1
- if TABLE 1 content column value doesn't exist in qa_postmetas
THEN
- INSERT (recursively)
INSERT INTO qa_posts (type, categoryid, userid, created, title, content, tags) 
(SELECT  'Q_QUEUED', '1', '3', NOW(), f.title, f.img, f.tagsv 
FROM dev.test1 f) 

Open in new window

and then
- GET inserted postid from qa_posts and insert values from table1 in table 3

qa_postmetas (postid <-- (this is what we have just inserted in first insert),
 title, <-- this is hard coded value 'qa_q_extra'
content <-- this is coming from table 1 (URL column)
) )
Comment
Watch Question

Ludwig DiehlSystems Architect

Commented:
I supposed you are talking about database tables, if so, then what kind of database ? (MySQL, MSSQL, Postgresql, etc).
Please give us more information

Author

Commented:
I am using MYSQL

thanks
Distinguished Expert 2019
Commented:
Are you trying to correct a structural error in the database?
i.e. you have interdependent tables but data was entered in a way that did not maintain relational integrity?

You can simply use PHP in a recursive loop retriving data from one table, running selects on the other and when no response, running the insets.  Before you go down this road, I would suggest you make sure your table definitions/constraints/relationships/triggers as well as what inserts/updates these corrected and are setup to maintain data integrity or you will have to repeat this process on a regular basis and have conditions such that data retrieved might be suspect at any point in time.
Most Valuable Expert 2011
Top Expert 2016
Commented:
If you're new to PHP and MySQL, this article can help you find some good "getting started" resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
Commented:
CalmSoul:

Is this a one-time maintenance operation, or something that will be used on a regular basis? Does it have to be implemented in PHP code recursively? It may be able to be done using a moderately complex SQL query, eliminating coding, debugging, and maintenance.

Regards,

Aielloj

Author

Commented:
its one time ...
Commented:
CalmSoul:

Thank you for the information.  Can you provide just a bit of detail of why this is needed?  I'm trying to find out if this is unexpected data population due to a data model error, upgrade to an existing system, etc.  If it's due to any type of error, you would do well to address them as per Arnold's earlier post.

I'll look into whether this can be done with a query.

Best regards,

AielloJ
Commented:
CalmSoul:

The column names on test1 table have been truncated.  Can you please post all the columns.  Also, can you provide a rough estimate of how many rows you expect this query to fetch and insert?

Best regards,

AielloJ