homepage Welcome to WebmasterWorld Guest from 54.242.231.109
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Duplicating MySQL Table Rows
duplicating table rows
Tourex




msg:3236362
 8:52 pm on Jan 29, 2007 (gmt 0)

I'm not sure whether this should be posted here on in databases, but it's a question about inserting records in a MySQL table using PHP.

I have a large table with some 50+ fields with an auto-increment primary key field. When a record is edited by a user, we need to duplicate the original record, and update the fields changed by the user.

My question is, what is the easiest way to capture and duplicate the original table row? Yes, I could read the record in and then loop through the $row[] array using this as the source of all fields (except the primary key), but it strikes me that there must be a simpler method.

Any tips would be much appreciated.

 

whoisgregg




msg:3236415
 9:45 pm on Jan 29, 2007 (gmt 0)

I think this is one of the uses of the INSERT SELECT syntax [dev.mysql.com]:

INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

Keep in mind the notes from the manual page, particularly:

To avoid ambigious column reference problems when the SELECT and the INSERT refer to the same table, provide a unique alias for each table used in the SELECT part, and qualify column names in that part with the appropriate alias.

coopster




msg:3236427
 9:53 pm on Jan 29, 2007 (gmt 0)

You will have to use a SELECT statement to get current values prior to update. UPDATE will return the number of rows updated, not the before/after values. And actually it would be mysql_affected_rows [php.net] that would return that type of value or information, not the query itself.

Why do you need to get and store the values? As a tracking system of sorts? If so, there is always the binary log and it's related utility to dig in and locate operations.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved