Jump to content

Copy records into the same DB


ppgpilot

Recommended Posts

I have a DB with 10 fields

 

Records are entered under a date

 

There can be 1 to 30 records for a given date

 

I need to copy all records of a given date with the 10 fields of each...

 

Into the same DB only changing the original date to a new date

 

Sounds easy, but so far I haven't been able to make it work.

 

Any nudge in the right directiion would be much appreciated.

 

 

Link to comment
Share on other sites

No idea how much you have already done, so on the whole the entire process will be as follows (assuming you are using PHP for this process):

 

[*]SELECT the records with the old dates "SELECT * FROM table WHERE date='[Date Here]'"

[*]Read all the columns in to PHP, and format them into an INSERT statement, changing the dates into the new dates

[*]Execute the INSERT statement

 

Hopefully that can get you started? If not, post what you have already tried and describe the problems you are facing.

Link to comment
Share on other sites

Thanks for the reply.

 

I should mention I am very new to loops…

 

I will have an array from the SELECT and I am using a WHILE statement to run through the array. So far so good

 

$sql=("select * from $tbl3 where day = '$org_date_unix' order by item");

$result = mysql_query($sql);

require 'my_error.php';

$works_call = mysql_num_rows($result);

 

while($rows = mysql_fetch_array($result))

{    $item = $rows['item'];  etc…}

 

I then display these on the page and all is well. But placing them back in is where I am having the problem.

 

Once I define the variables - maybe they need to be arrays since there will be an numberof them - I need to loop them back into the database with the newly defined date variable to create the same number of records with 10 fields each. Yes?

 

Would another WHILE statement be best, a FOREACH or a FOR loop be the best to use? - and I will begin my research from there.

 

Would I INSERT within the original WHILE statement that selected the records? Or outside in a different loop.

 

Can’t quite get my head around this yet. Knowing the concept and best approach / type of loop for this operation will save me a bunch of time.

 

Thanks!

 

Link to comment
Share on other sites

Okay, the code looks fine so far. I haven't tested the code below, but try replacing everything in your while loop with that.

 

array_shift($row);
$row['date'] = "[date here]";
foreach($row as $k=>$v)
$row[$k] = '"'.mysql_real_escape_string($v).'"';
$insert = mysql_query("INSERT INTO table_name VALUES(null, ".implode(',',$row).")");

 

Make sure that you (1) replace $row['date'] with the correct name of your date row, (2) replace [date here] with whatever new date you have, and (3) change the table name.

 

Basically, I am assuming that your first row is your id column which you don't want to touch, so using array_shift, remove that from your array. Then, the next line replaces the date with your new date. The foreach loop just adds quotes around the values and uses mysql_real_escape_string (if you need them). And the insert statement brings it all together.

 

It might be slightly faster to combine the insert statements, but you get the general idea of how it works.

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.