RE: [PHP] Duplicating a Record in MySQL
| From: | Roger Lewis | Date: | Sat, 19 Oct 2002 20:45:55 +0000 |
| Subject: | RE: [PHP] Duplicating a Record in MySQL | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-120630@lists.php.net to get a copy of this message | ||
-----Original Message-----
From: John W. Holmes [mailto:holmes072000@charter.net]
Sent: Saturday, October 19, 2002 12:52 PM
To: 'Roger Lewis'; 'Php-General'
Subject: RE: [PHP] Duplicating a Record in MySQL
* >> It's not strictly php, but does anyone know an easy way to
duplicate a
> >> record in MySQL. It would be nice if you could write something
like:
> >>
> >> COPY * from table WHERE id = 6;
> >>
> >> and have it insert a new record into the table with the next id
> >>number.
>
> >I think that if you have to do this, you've got design issues. Why
not
> >just keep a "count" column in the row and increment that, instead of
> >duplicating the row.
>
> >---John Holmes...
> John,
> Here's the application: I'm creating a daily report online, eg. with
> Title,
> Report no., Date, Author, Subject Headings, Text, etc., etc.. The
report
> is
> stored in a MySQL table. When it comes time to write a new report, I
> would
> like to be able to call up an existing report, upon which to base the
new
> one, and copy the record. Then make changes where necessary and
update
> the
> record.
So have PHP select out the row you want to "copy", make your changes to
it with PHP or a form, then have PHP insert a new row into the database.
Why copy data when you're going to change it.
---John Holmes...
Well, you're probably right. I've been working with Adobe GoLive, and have
been using the CopyFormData command, so that's where I got the idea. I ran
into trouble with GoLive when I complicated my report form by adding file
attachments. Anyway, here's a condensed version of code base on what
Stephen Knight suggested. (Thanks, Stephen). I'll work on this to try and
avoid the copying step as you are suggesting.
$sql = "select * from daily_reports where report_no = '$report_no' ";
$result = mysql_query($sql);
$x = mysql_fetch_row($result);
$fields = "report_no, order_no, report_date, project_no, project_name,
client, title, subtitle, author, company, summary_heading,
summary, section1_heading, section1";
$values = " '$x[1]', '$x[2]', '$x[3]', '$x[4]',
'$x[5]', '$x[6]', '$x[7]',
'$x[8]',
'$x[9]', '$x[10]', '$x[11]', '$x[12]', '$x[13]',
'$x[14]' ";
// Note: There must be a way to have PHP generate the previous line, but I
don't know how.
$sql = "INSERT INTO daily_reports ($fields) VALUES ($values)";
mysql_query($sql);
Thanks, Roger