Copy multiple rows php mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nbradshaw
Forum Newbie
Posts: 1
Joined: Sun Nov 16, 2008 5:41 pm

Copy multiple rows php mysql

Post by nbradshaw »

Hello -

I am trying to find a script that will duplicate/copy rows based on user input
For example:

I would like to have a form that has html inputs for:
1. id (primary key for row)
2. How many times the user would like to duplicate/copy that specific row
3. Output a success message with the new row id numbers

php5 mysql5

Many thanks for any help!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Copy multiple rows php mysql

Post by josh »

A subquery

INSERT INTO _____ SELECT `fields` FROM ___
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Copy multiple rows php mysql

Post by califdon »

Your question raises concern over what you are trying to accomplish. Under most conditions, duplicating rows in a table is not a desirable objective. It is usually a sign that the table is not normalized, else you wouldn't need to be doing this.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Copy multiple rows php mysql

Post by josh »

That's not true. Very well possible but not necessarily something you can assume. What if the end user specifically needs to make a copy of a record, ala the "prototype" design pattern?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Copy multiple rows php mysql

Post by alex.barylski »

It is usually a sign that the table is not normalized, else you wouldn't need to be doing this.
I don't personally consider duplication of entire record qualifies as an example of when a schema needs to be normalized. Normalization typically means removing redundancy, sure, but redundancy and duplication are not the same thing in this context.

Although I will agree that under most circumstances duplication is not desired either.

Copying records might be useful in the case of baking up to another table or when you want to copy a single record and modify only some fields.
That's not true.
In his defense he did state "most circumstances" not "all circumstances" which would be accurate. Without more details as to what OP is trying to achieve it's difficult to say whether this is bad practice.

Cheers,
Alex
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Copy multiple rows php mysql

Post by josh »

As long as a new pk is assigned it doesn't violate normalization, if its within the same table. Entities are identified by pk not attributes. Attribute duplication != de-normalization. After all if i COPY a file in windows then modify the source, it doesn't replicate my change into the copy. The copy is a clone, it's own entity, the fields are the values. It's fine to duplicate values, as long as entities are not duplicated... but all of this is outside of the original question which was regarding the syntax I believe :lol:

Edit: actually there was no original question :crazy:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Copy multiple rows php mysql

Post by califdon »

califdon wrote:Your question raises concern over what you are trying to accomplish. Under most conditions, duplicating rows in a table is not a desirable objective. It is usually a sign that the table is not normalized, else you wouldn't need to be doing this.
Gee, I woke you guys up! If you will re-read what I said, I think you will find it hard to disagree. In hundreds of cases I have seen in forums and a decade of teaching database courses in college, nearly every time someone "wants" to duplicate the data in rows in the same table, it is a bad case of failure to normalize even to 2NF. The issue is that precisely the same set of attributes is no longer uniquely identified by a primary key. In my response, I did acknowledge that this is not necessarily true in every case. But to me, at least, it does "raise concern."

At any rate, I am glad to see so many people interested in an issue like this. It is really fundamental to a useful database design, and even if we may disagree on a particular question, it's important to have such a discussion.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Copy multiple rows php mysql

Post by josh »

I guess I wasn't disagreeing with you then, I'm just reiterating its not necessarily bad, its not like he was using globals.. but yeah I agree row duplication has abuse potential
Post Reply